Sunday, August 12, 2007

Web Service > Consuming > Code Listing

CREATING A WEB SERVICE CLIENT
• Creating a client for the Web service in Visual Studio is same as creating a web application
• To use a web service, it is required to import the namespace, eg:
• using WebClient.localhost; WebClient is the name specified by the user to the web service
• Sample code that populates a GridView through DataSet returned by the web service

-----------------------------
using WebClient.localhost;

EmployeesService proxy = new EmployeesService();
proxy.Timeout = 3000; // 3 seconds
DataSet ds = null;

try
{
ds = proxy.GetEmployees();
}

catch(System.Net.WebException err)
{
if(err.Status == WebExceptionStatus.Timeout)
lblErr.Text=”Web Service Timed out”;
else
lblErr.Text=”Unknown Error occurred.”;
}
if(ds!=null)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
------------------------

No comments: