Calculator WebService

Calculator WebService Consume in yourApplication

Steps :
New Project- Add Webservice(asmx) name- CalculatorWebservice
Adding Methods or Function

Code
-------------------------------
CalculatorWebservice.cs
-------------------------------

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

/// <summary>
/// Summary description for CalculatorWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 CalculatorWebService : System.Web.Services.WebService
{

    public CalculatorWebService()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public int Add(int firstnumber, int secondnumber)
    {
        return firstnumber + secondnumber;
    }

    [WebMethod]
    public int Sub(int firstnumber, int secondnumber)
    {
        return firstnumber - secondnumber;
    }

    [WebMethod]
    public int Mul(int firstnumber, int secondnumber)
    {
        return firstnumber * secondnumber;
    }

    [WebMethod]
    public int Div(int firstnumber, int secondnumber)
    {
        return firstnumber / secondnumber;
    }

    
}

Create a Web Application and Add Reference in Web Application

-------------------
Calculator.aspx
-------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Calculator.aspx.cs" Inherits="Calculator" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>
                    <b>First Number</b>
                </td>
                <td>
                    <asp:TextBox ID="txtfirst" runat="server"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <td>
                    <b>Second Number</b>
                </td>
                <td>
                    <asp:TextBox ID="txtsecond" runat="server"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <td>
                    <b>Result</b>
                </td>
                <td>
                    <asp:Label ID="lblresult" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnadd" runat="server" Text="Add" OnClick="btnadd_Click" />
                    <asp:Button ID="btnsub" runat="server" Text="Sub" OnClick="btnsub_Click"  />
                    <asp:Button ID="btnmul" runat="server" Text="Mul" OnClick="btnmul_Click"  />
                    <asp:Button ID="btndiv" runat="server" Text="Div" OnClick="btndiv_Click" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>



-----------------
Calculator.cs
-----------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnadd_Click(object sender, EventArgs e)
    {
        CalculatorServices.CalculatorWebServiceSoapClient client =
            new CalculatorServices.CalculatorWebServiceSoapClient();
        int result = client.Add(Convert.ToInt32(txtfirst.Text), Convert.ToInt32(txtsecond.Text));

        lblresult.Text = result.ToString();
    }

    protected void btnsub_Click(object sender, EventArgs e)
    {
        CalculatorServices.CalculatorWebServiceSoapClient client =
            new CalculatorServices.CalculatorWebServiceSoapClient();
        int result = client.Sub(Convert.ToInt32(txtfirst.Text), Convert.ToInt32(txtsecond.Text));

        lblresult.Text = result.ToString();
    }

    protected void btnmul_Click(object sender, EventArgs e)
    {
        CalculatorServices.CalculatorWebServiceSoapClient client =
            new CalculatorServices.CalculatorWebServiceSoapClient();
        int result = client.Mul(Convert.ToInt32(txtfirst.Text), Convert.ToInt32(txtsecond.Text));

        lblresult.Text = result.ToString();
    }

    protected void btndiv_Click(object sender, EventArgs e)
    {
        CalculatorServices.CalculatorWebServiceSoapClient client =
            new CalculatorServices.CalculatorWebServiceSoapClient();
        int result = client.Div(Convert.ToInt32(txtfirst.Text), Convert.ToInt32(txtsecond.Text));

        lblresult.Text = result.ToString();
    }
}












Comments

Popular posts from this blog

Forms

How to ReadHtmlFile in string