Click here to Skip to main content
Licence CPOL
First Posted 29 Oct 2005
Views 35,453
Downloads 326
Bookmarked 22 times

Using Web Services to Bind a DataSet to a DataGrid

By | 29 Oct 2005 | Article
Create WebServices and bind a DataSet to a DataGrid.

Introduction

This article will be useful for beginners for learning about Web Services. Create a local database (MS-Access) and create a new Web Service application to connect to this database. Create the local database connection as a class file.

// Beginning of Class File
Imports System.Data.OleDb
Public Class DBConnection
    Dim str As String
    Dim con As OleDbConnection

    Public Function OpenConnection() As String
        con = New OleDbConnection
        con.ConnectionString = DBopen()
        con.Open()
    End Function

    Public Function DBopen() As String
        str = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;" & _ 
              "Data Source=D:\AndavarSalary.mdb;"
        Return str
    End Function
End Class
// End of the Class File

Open the Web Services form "WService.asmx" to create a web method. Import the required namespaces and add the required classes.

Imports System.Web.Services
Imports System.Data.OleDb
' Beginning of the webmethod coding - method name is "Addition"
<WebMethod()> _
Public Function Addition(ByVal a As String) As DataSet
    Dim DBconn As OleDbConnection ' Declare Database connection object.
    Dim cls As DBConnection ' connect the database connectionstring class file 
    cls = New DBConnection 
    DBconn = New OleDbConnection
    DBconn.ConnectionString = cls.DBopen
    Dim ds As New DataSet
    Dim sql As String
    If a <> 0 Then
        sql = "select * from tbl_test where s_no='" & a & "'"
    ElseIf a = 0 Then
        sql = "select * from tbl_test"
    End If
    Dim apt As OleDbDataAdapter
    apt = New OleDbDataAdapter(sql, DBconn)
    apt.FillSchema(ds, SchemaType.Source, "Load")
    apt.Fill(ds, "Load")
    Return ds
End Function
' End of the Web method

The above code is only on the Web Service's web pages. Now the Web Service is successfully created. After executing your Web Sservice application, you can get the URL of the page: http://localhost/Sample/WService.asmx.

Sample screenshot

Next create your WebForm on another machine for accessing the local database values. The following steps can be followed..

Go to your web application and add the required web references. Add the URL, paste this URL like http://system IP Address/Sample/WService.asmx.

Sample screenshot

Open your webform1.aspx, design the form: just put in a TextBox and a Button.

Go to the Button's Click event and write this code:

Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    'Declare dataset object
    Dim ds As New DataSet
    ' Create Webservces Reference Object
    Dim wsdataset As WebReference.WService
    wsdataset = New WebReference.WService
    ' Check whether Input Textbox value 
    If TextBox1.Text <> "" Then
        ds = wsdataset.Addition(TextBox1.Text)
    Else
        ds = wsdataset.Addition(0)
    End If
    'Bind the Dataset value to Datagrid
    DataGrid1.DataSource = ds
    DataGrid1.DataBind()
End Sub

Execute your web application. Now you will be able to get the values in the local database tables. If you need any more information on this, please let me know.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Srinivasan Venkatachalam

Web Developer

India India

Member

Hi i am srinivasam venkatachalam. At present i am working as a Senior Software Engineer in .Net Technologies.
 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFormatting PinmemberBassam Saoud7:34 19 Jan '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 30 Oct 2005
Article Copyright 2005 by Srinivasan Venkatachalam
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid