Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to bind data to Web Service and use this Web Service in ASP.NET

Author: Alanna Kremer
Download Source Code :

How you can bind data to a Web Service will be explained in this article and we will also learn how we can use this data bind Web Service.


Web Services are created to bind data because these are used by consumers to work with data. You can see articles on DevASP.NET about Web Services. In this article, I will show you simple example of data binding to web service. This data binding procedure can be complex but this example is just for learning purposes.

Follow below steps to create a web service and bind data to it.
  1. Create New Web Site in Visual Studio 2010
  2. Select .NET Framework 3.5
  3. Select ASP.NET Web Service either in Visual C# or in Visual Basic
  4. Add a Class and rename it as ProductsClass
  5. Import namespaces in ProductsCass

    using System.Data;
    using System.Data.SqlClient;
    Imports System.Data
    Imports System.Data.SqlClient
  6. Write below function in ProductsClass to get data
    public static DataSet ProductsData() {
        string sqlQuery = "Select * From Products";
        string connString = "Data Source=Local;Initial Catalog=NORTHWND;Integrated Security=True";
        SqlConnection connection = new SqlConnection(connString);
        SqlCommand command = new SqlCommand(sqlQuery, connection);
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        return ds;
    Public Shared Function ProductsData() As DataSet
        Dim sqlQuery As String = "Select * From Products"
        Dim connString As String = "Data Source=Local;Initial Catalog=NORTHWND;Integrated Security=True"
        Dim connection As New SqlConnection(connString)
        Dim command As New SqlCommand(sqlQuery, connection)
        Dim da As New SqlDataAdapter(command)
        Dim ds As New DataSet()
        Return ds
    End Function
    As you can see I have used Products table of NORTHWIND database. This function returns DataSet. I have written a query and set connection to my Server. Then I have created a SqlCommand object by passing query and connection as parameters. Then I have created a SqlDataAdapter object, a DataSet object and fill DataSet object with SqlDataAdapter object. 
  7. Open Service.cs file and import namespace

    using System.Data;

    Imports System.Data
  8. Now write below function in Service.cs file
    public DataSet GetProducts() {
        DataSet ds = ProductsClass.ProductsData();
        return ds;
    <WebMethod()> _
    Public Function GetProducts() As DataSet
        Dim ds As DataSet = ProductsClass.ProductsData()
        Return ds
    End Function
    This function in our Web Service code file returns DataSet. I have created a DataSet object and assigned the data of ProducsData() function of ProductsClass. We will call this method to get data in our consumer web site.
  9. See this Web Service in browser and copy the URL of Web Service. We need this URL for service reference to consume this Web Service.

 Now follow below steps to consume this Web Service and get data from this Web Service function.

  1. Open New Web Site
  2. Create an ASP.NET Empty Website either in Visual Basic or Visual C#
  3. Right click on the Website in Solution Explorer
  4. Select Add Service Reference
  5. Select Advanced option in Add Service Reference window
  6. Select Add Web Reference in Service Reference Settings
  7. Enter the copied URL in Add Web Reference window
  8. Change the Web reference name as DataBindWebService and click Add Reference
  9. Import namespace for the Web Service

    using DataBindWebService;
    Imports DataBindWebService
  10. Drag and Drop a GridView in aspx page
    <asp:GridView ID="GridView1" runat="server">
  11. Write below code in Page_Load method of code behind file

    protected void Page_Load(object sender, EventArgs e) {
        Service service = new Service();
        GridView1.DataSource = service.GetProducts();

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim service As New Service
        GridView1.DataSource = service.GetProducts()
    End Sub
    Create a new instance of Web Service and call the GetProducts() method. Bind the data to GridView.
  12. View website in browser, you will see the result of data bound Web Service method.
Add Article Comment:
Name :
Email Address :
Comments :
<< Web Services and standard web protocols

Disclaimer - Privacy
© 2002-2017