XML Web Services
August 25, 2011 Leave a comment
In this section we will build a more interesting Web service that returns a ADO .NET DataSet, containing the full set of records from a table. We will create our own database table and access the data from the table with this Web service. To Start, open Microsoft Access and create a new Database named Currency. Create a new table Table1 and add three columns named, Country Code, Country Name and Currency. Enter some values in the table and close it. Open Visual Studio .NET and select ASP .NET Web service from the projects type template. Drag a OleDb connection from the Data tab in the toolbox and using the properties window build a connection string that connects to the Currency database which we created. Switch to the code view and start writing the following code.
Imports System Imports System.Web.Services Imports System.Data.OleDb ‘import this namespace as we are working with an OleDb source<WebService(Namespace := “http://tempuri.org/”)> _Public Class Service1 Inherits System.Web.Services.WebService #Region ” Web Services Designer Generated Code ” #End Region <WebMethod()> Public Function GetData() As DataSet |
Consuming the Service
Once you finish with coding the Web service we need to consume this service. To do that, open a new Windows Application and from the toolbox drag a DataGrid and a Button. Our intention here is to load the data from Table1 in the Currency database into the DataGrid when we click the Button. Now, add a Web reference to the Web service by selecting Reference->Add WebReference in the Solution Explorer Window. Enter the URL of the service in the address bar and click “Add Reference“. That adds a reference to the Web Service. Now double-click on the Button and write the following code.
Public Class Form1Inherits System.Windows.Forms.Form
#Region ” Windows Form Designer generated code ” #End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e_ |
Once you finish with the code, run the Windows Application and click on the Button. The data you entered in Table1 of the Currency database will be displayed in the datagrid. The difference, we are accessing the data with a Web service. The image below displays that.