How to populate web server controls in ASP.Net applications from a web service

In this post I would like to present you with a very simple example on how to create a simple web service that access data from a database.

Then we will create a consuming application that will consume the web service. I will use VS 2010 Ultimate edition and C# as the development language.

I know that web services are explained in many places over the web but I will have a go.

They are small units of code that handle specific tasks (they convert kilometers to miles ) and are based on XML communication protocols.

They are great because they are independent of operating systems,languages,systems and devices.

Before you read on, I assume that you understand terms like SOAP, WSDL and XML .

1) Use VS 2010/2008/2005. Express editions will suffice. I am going to use the Pubs database. You can download the installation scripts of the Pubs database from here.

I am going to retrieve all the data from the Authors table.

2) Create a new project and choose asp.net web service application. You have this option from the available templates when you click File->New Project.

3) Add these namespaces at the top of the Service.cs

using System.Data;
using System.Web.Configuration;
using System.Data.SqlClient;
 

Also note that our web service class inherits from WebService base class and not from the Page class

public class Service : System.Web.Services.WebService

4) In the web.config file in the <connectionStrings> add

  <connectionStrings>
    <add name="pubs" connectionString="Data Source=.;
Initial Catalog=pubs;Integrated Security=True"
        providerName="System.Data.SqlClient" />
  </connectionStrings>

 

5) Delete the Helloworld method. Under the [WebMethod]

 public DataSet GetAuthors()
    {
        SqlConnection conn = new SqlConnection
(WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString);
        SqlDataAdapter myAdapter;
        DataSet productDataSet;
        string commandString = "Select * from Authors";
        myAdapter = new SqlDataAdapter(commandString, conn);
        productDataSet = new DataSet();
        myAdapter.Fill(productDataSet);
        return productDataSet;


    }

I just get the connection string from the web.config using the WebConfigurationManager class and create a new connection object. I store my SQL Query in a string and then use the connection object and the string that stores the SQL query as paramaters when creating an Adapter object.Then I create a dataset and fill this dataset using the adapter object.

6) Build and run your application. You will see the service running and Invoke the GetAuthors method. Make sure it works.

7) Add a new item to your project, a web form.Leave the default name.

8) Add a Gridview web server control in the default.aspx page. Now we need to add a reference to the web service.Add Web Reference-> Select Web services in this solution and type in the web reference the name, AuthService

9) In the Page_Load event handling routine, type

 AuthorService.Service auth = new AuthorService.Service();
 GridView1.DataSource = auth.GetAuthors();
 GridView1.DataBind();

We just create an object of the AuthorService.Service class (auth) and then it is very easy(we call the GetAuthors() method) to bind the results to the GridView.

10) Run your application and see the author data populating the GridView control.

Email me if you want the source code.

Hope it helps!!!!

4 Comments

Comments have been disabled for this content.