Monday 9 April 2012

Web services Example || Add two numbers using Web services in Asp.net.

To implement this concept you need to follow the below steps :

Step1 :
Create a new Asp.net website in Visual Studio and write the following html code in the design part of the Default.aspx page.
 

Design Page :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <table class="style1">
        <tr>
            <td class="style2">
                First Number :</td>
            <td>
                <asp:TextBox ID="txtnum1" runat="server"></asp:TextBox>
            </td>
        </tr><tr>
            <td class="style2">
                Second Number :</td>
            <td>
                <asp:TextBox ID="txtnum2" runat="server"></asp:TextBox>
            </td>
        </tr><tr>
            <td class="style2">
                &nbsp;</td>
            <td>
                <asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Add"
                    Width="66px" />
            </td>
        </tr><tr>
            <td class="style2">
                Result :</td>
            <td>
                <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
            </td></tr>
    </table>
    </form>
</body>
</html>



Step2:
Now open the Default.aspx.cs page and write the following source code.

Default.aspx.cs code :
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
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        WebService ws = new WebService();
        lblResult.Text = ws.add(Convert.ToInt32(txtnum1.Text), Convert.ToInt32(txtnum2.Text)).ToString();
    }
}



Step3:
Now build the Solution and Debug it for the output.

Output :



No comments:

Post a Comment