Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Web Services >> Creating a web service for performing arithmetic operations
Creating a web service for performing arithmetic operations
Creating a web service for performing arithmetic operations


by mohan kumar on 5/8/2012 8:10:30 PM

Views: 2010       Comments: 1

Creating a web service for performing arithmetic operations

Introduction:

I am creating a web service for performing arithmetic operations or say a calculator. Then, I will use this service in a windows based application to perform arithmetic operations like a calculator. So, let's create a web service. Following is the Code Snippet.

Now replace the code on .asmx.cs page with the following code.

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;

namespace
calc
{
    /// <summary>
    /// Summary description for CalcWebService
    /// </summary>
    [WebService(Namespace = "mycalculatorexample.org")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class CalcWebService : System.Web.Services.WebService
    {
       [WebMethod]
        public string calculate(string first, string second, char sign)
        {
            string result;
            switch (sign)
            {
                case '+':
                    {
                        result = (Convert.ToInt32(first) + Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '-':
                    {
                        result = (Convert.ToInt32(first) - Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '*':
                    {
                        result = (Convert.ToInt32(first) * Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '/':
                    {
                        result = (Convert.ToInt32(first) / Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '%':
                    {
                        result = (Convert.ToInt32(first) % Convert.ToInt32(second)).ToString();
                        break;
                    }
                default:
                    result = "Invalid";
                    break;
            }
            return result;
        }
    }
}

 
Run the application.

Output:
Creating a web service for performing arithmetic operations

Go to test page by clicking at "calculate" -> do the entry and click the "invoke" button.
 
Creating a web service for performing arithmetic operations

It will show the result like the below figure. (After clicking the "invoke" button)

Creating a web service for performing arithmetic operations

Similarly, you can perform different operations like addition, subtraction, multiplication, division and modulo division. Now we consume this web service into window form application.

Create a windows application and add a service reference (You can get help here ). After adding the reference, go to the design page and arrange the UI controls like in the below figure.

Creating a web service for performing arithmetic operations

Now, write the following code in the .cs file.

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
 
namespace
Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string i, j;
        static char c;
        private void btnthree_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(3);
        }
        private void btnone_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(1);
        }
        private void btntwo_Click(object sender, EventArgs e)
       {
            textBox1.Text += Convert.ToString(2);
        } 
        private void btnfour_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(4);
        } 
        private void btnfive_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(5);
        } 
        private void btnsix_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(6);
        } 
        private void btnseven_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(7);
        } 
        private void btneight_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(8);
        } 
        private void btnnine_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(9);
        } 
        private void btnzero_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(0);
        } 
        private void btnplus_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '+';
        }
        private void btnminuse_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '-';
        }
        private void btnmultiply_Click(object sender, EventArgs e)
        {
           i = textBox1.Text;
            textBox1.Text = "";
            c = '*';
        }
        private void btndivide_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '/';
        }
        private void btnmodulo_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '%';
        }
        private void btnequal_Click(object sender, EventArgs e)
        {
            j = textBox1.Text;
            calclocalhost.CalcWebService obj = new calclocalhost.CalcWebService();
            textBox1.Text = obj.calculate(i, j, c);
        }
    }
}

Run the application.

Output:
Creating a web service for performing arithmetic operations
You can work as calculator.

Report Abuse Form
Reason:    
 


Web Service
by Mark Toy 5/8/2012 8:20:46 AM
Keep writing Mohan Kumar... nice article
Report Abuse
Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by mohan kumarRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 3464
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.