Friday, September 11, 2009

Consume/Calling the Web Service from the Client Script in C#

 We can use web service in any type of application. Here we will create an application .Net web application. For that, we will follow the following steps:

Step1: Right-click on the Solution explorer->Add->New Project as shown in the below screenshot:

How to create Web Service

Step2: After clicking on the New Project a new window will open as shown in the below screenshot:

How to create Web Service

Step 3. After this, to communicate with the web service, we have to create a proxy class. To create the proxy class, we have to follow the following steps:

Proxy Class

For creating the proxy class, we have to the right click on the References->Select Add Service Reference as shown in the below screenshot:

How to create Web Service

After clicking on the Add Service Reference, a new window will appear as shown in the below screenshot:

How to create Web Service

Explanation of the above window:

Address: Here, we will paste the URL of the web service created by us and then click on Go.

After clicking on the Go button, this will search the provided address.

Namespace: in the namespace, we will provide the name of the Service and then click on the OK button. This will add the reference to the web service in the project.

Now we will add the Web Form in our application. For that, we will follow the following steps:

1. Right-click on the project in the solution explorer-> select add->choose web form as shown in the below screenshot:

How to create Web Service

After clicking on the Web Form, this will open a new window where we have to provide the name of the web form as shown in the below screenshot:

How to create Web Service

Here in the above screenshot, we gave the name of the web form and then click on OK.

In the Web Form, we will create a table to use the predefined method with the help of the Web Service.

In the WebForm1.aspx, we have done the following coding as shown in the below code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CalculatorWebApplication.WebForm1" %>    
  2. <!DOCTYPE html>    
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <table style="font-family:Arial">  
  10.             <tr>  
  11.                 <td>  
  12.                     <b>First Number</b>  
  13.                 </td>  
  14.                 <td>  
  15.                     <asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox>  
  16.                 </td>  
  17.             </tr>  
  18.             <tr>  
  19.                 <td>  
  20.                     <b>Second Number</b>  
  21.                 </td>  
  22.                 <td>  
  23.                     <asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox>  
  24.                 </td>  
  25.             </tr>  
  26.              <tr>  
  27.                 <td>  
  28.                     <b>Result</b>  
  29.                 </td>  
  30.                 <td>  
  31.                     <asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label>  
  32.                 </td>  
  33.             </tr>  
  34.             <tr>  
  35.                 <td>  
  36.                     <asp:Button ID="btnadd" runat="server" Text="Add" OnClick="btnadd_Click" />  
  37.                     </td>                    
  38.             </tr>  
  39.         </table>  
  40.     </form>  
  41. </body>  
  42. </html>  

In the above code, create a table where we take the two textboxes, textbox1, for entering the firstnumber and the textbox2 for entering the SecondNumber. Create a button with the help of which we will add the two numbers.

The designing view of the above code is as shown in the below screenshot:

How to create Web Service

Here we have taken two textboxes for entering the firstnumber and secondnumber. The add button is used to add the value of both the textbox.

After double-clicking on the Add button, this will switch us to the coding page. The page is WebForm.aspx.cs.

To use the method of the Web Service, we will do the following code on the button click.

WebForm.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System. Linq;  
  4. using System. Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;    
  7. namespace CalculatorWebApplication  
  8. {  
  9.     public partial class WebForm1 : System.Web.UI.Page  
  10.     {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {   
  13.         }    
  14.         protected void btnadd_Click(object sender, EventArgs e)  
  15.         {  
  16.             CalculatorService.CalculatorWebServiceSoapClient client = new CalculatorService.CalculatorWebServiceSoapClient();  
  17.                 int result= client.Add(Convert.ToInt32(txtFirstNumber.Text),  Convert.ToInt32(txtSecondNumber.Text));  
  18.             lblResult.Text = result.ToString();  
  19.         }  
  20.     }  
  21. }  

Now we will press F5 to start the web service. The output will look like as shown in the below screenshot:

OutPut

How to create Web Service

Wrap Up

Here we got the benefit of the web service in this application. We didn't write any logic for the addition of the two numbers. Here in our calculator web application, we just used the method of the web service. We didn't use any logic for the addition. The above screenshot shows the output of the addition of two numbers, i.e., 23.

No comments:

Popular Posts