Friday 14 February 2014

web service example in asp.net

web service example in asp.net
OR
Addition,subtraction, multiplication and division operation
using web service


if you want to create your own web service than do following

--> first add one web service from right click on solution explorer(current project)
--> set name as ws_calc
--> next following code paste in your web service
------------------------------------------------------------------------------------------------


//Default method
    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public int add(int a, int b)
    {
        return a + b;
    }

    [WebMethod]
    public int sub(int a, int b)
    {
        return a - b;
    }

    [WebMethod]
    public int mul(int a, int b)
    {
        return a * b;
    }

    [WebMethod]
    public int div(int a, int b)
    {
        return a / b;
    }

--> copy the following code in your .aspx page
------------------------------------------------------------------------------------------------


<div>
<table class="style1">
<tr>
<td colspan="2">
    <asp:label id="Label11" runat="server" font-names="Arial" font-size="X-Large" forecolor="#0033CC"
        text="Web service Example&nbsp;"></asp:label>
</td>
</tr>
<tr>
<td class="style2">
    &nbsp;
</td>
<td>
    &nbsp;
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label1" runat="server" text="Enter value"></asp:label>
</td>
<td>
    <asp:textbox id="TextBox1" runat="server">
    </asp:textbox>
</td>
</tr>
<tr>
<td class="style2">
    &nbsp;
</td>
<td>
    &nbsp;
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label2" runat="server" text="Enter value"></asp:label>
</td>
<td>
    <asp:textbox id="TextBox2" runat="server">
    </asp:textbox>
</td>
</tr>
<tr>
<td class="style2">
    &nbsp;
</td>
<td>
    &nbsp;
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:button id="Button1" runat="server" text="Submit" onclick="Button1_Click" />
</td>
<td>
    &nbsp;
</td>
</tr>
<tr>
<td class="style2">
    &nbsp;
</td>
<td>
    &nbsp;
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label7" runat="server" text="Addition" font-names="Arial" forecolor="#003300">
    </asp:label>
</td>
<td>
    <asp:label id="Label3" runat="server" forecolor="Red"></asp:label>
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label8" runat="server" text="Substraction" font-names="Arial" forecolor="#003300">
    </asp:label>
</td>
<td>
    <asp:label id="Label4" runat="server" forecolor="Red"></asp:label>
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label9" runat="server" text="Multiplication" font-names="Arial" forecolor="#003300">
    </asp:label>
</td>
<td>
    <asp:label id="Label5" runat="server" forecolor="Red"></asp:label>
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label10" runat="server" text="division" font-names="Arial" forecolor="#003300">
    </asp:label>
</td>
<td>
    <asp:label id="Label6" runat="server" forecolor="Red"></asp:label>
</td>
</tr>
<tr>
<td class="style2" align="right">
    &nbsp;
</td>
<td>
    &nbsp;
</td>
</tr>
</table>
</div>

--> next following code paste in your code behind file(.aspx.cs file)
------------------------------------------------------------------------------------------------


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

public partial class _Default: System.Web.UI.Page
{
    WS_calc ws1 = new WS_calc();// This line for create webservice object
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    if (TextBox1.Text == "" || TextBox2.Text == "")
    {
    }
    else
    {
        int a = Convert.ToInt32(TextBox1.Text);
        int b = Convert.ToInt32(TextBox2.Text);

        Label3.Text = ws1.add(a, b).ToString(); //calling web service for Addition
        Label4.Text = ws1.sub(a, b).ToString(); //calling web service for Substraction
        Label5.Text = ws1.mul(a, b).ToString(); //calling web service for Multiplication
        Label6.Text = ws1.div(a, b).ToString(); //calling web service for Division
    }
    }
}

No comments:

Post a Comment



Asp.net tutorials