Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework

Sponsors

About Me

Great Blogs

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!!!!

Comments

Aidan said:

That is a great post!!! It helped a newbie a miself.

# September 25, 2010 11:00 AM

Allister said:

Great post man,thanks

# September 25, 2010 11:42 AM

dj said:

hi,show me you code please,thanks very much! duanju91@163.com

# December 8, 2011 11:39 PM

Addy said:

Thanks for sahring. What a pleasure to read!

# December 15, 2011 11:45 PM