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

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

Author: Alanna Kremer
Download Source Code : 1327_BindDataToWebService.zip

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

    C#
     
    using System.Data;
    using System.Data.SqlClient;
     
    VB.NET
     
    Imports System.Data
    Imports System.Data.SqlClient
     
     
  6. Write below function in ProductsClass to get data
     
    C#
     
    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();
        da.Fill(ds);
        return ds;
    }
     
    VB.NET
     
    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()
        da.Fill(ds)
        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
     
    C#

    using System.Data;
     
    VB.NET

    Imports System.Data
     
  8. Now write below function in Service.cs file
     
    C#
     
    [WebMethod]
    public DataSet GetProducts() {
        DataSet ds = ProductsClass.ProductsData();
        return ds;
    }
     
    VB.NET
     
    <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

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

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

    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()
        GridView1.DataBind()
    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 DevASP.net