To consume a Web Service we need to create an Asp.Net web
application. After creating an application we should add a web reference (Add
web Reference option will come if we right click on Reference In the solution
explorer window). In the web reference dialog box the following should be
pasted in the URL selection box:
http://localhost/ArticleWS/Service1.asmx
Figure 3
Now by clicking on Add Reference button the web reference
will be added.
In the web.config file we should add the following as specified
in Listing 5.
Listing 5
<appSettings>
<add key = "CString" value = "Server=Sanjit; Database=Test; User ID=sa; Password = sa;"></add>
</appSettings>
In WebForm1 we should add the following as specified in
Listing 6 in the Page Load event code.
Listing 6
string con=ConfigurationSettings.AppSettings["CString"].ToString();
string sql="Select * from customer";
localhost.Service1 ws=new localhost.Service1();
DataSet DS;
DS=ws.Populate(con,sql);
DataGrid1.DataSource=DS;
DataGrid1.DataBind();
Note that before adding the above code to our Web Service
implementation, we need to add a DataGrid control in the WebForm. Then add the
following namespace as specified in Listing 7.
Listing 7
using System.Configuration;
After adding all the above mentioned code we can run the
application. Then the grid would be populated with the data from the customer
table. To test the Add WebMethod, add the following as specified in Listing 8.
Listing 8
localhost.Service1 ws=new localhost.Service1();
int a=Convert.ToInt32(TextBox1.Text);
int b=Convert.ToInt32(TextBox2.Text);
TextBox3.Text=ws.Add(a,b);
We should take three textbox controls and a Button Control. On
Button Click event we have written the above code. If we run the application
and input two integer values in two textboxes, the sum of two integers will be
displayed in third text box after clicking on button.