Using a Web Service as a Data Source


You want to use a web service as the data source for a client application.


Create a web service that returns a DataSet to a client, and then invoke the web service from the client to retrieve the DataSet .

The XML web service code contains one method:

LoadOrders( )

Creates a DataSet containing all Orders and Order Details data from Northwind. A DataRelation is created relating the tables. The DataSet is returned by the method.

The client-side code instantiates the web service class and calls the LoadOrders( ) method to create a DataSet containing the Orders and Order Details data from Northwind. The default view of the Orders table is bound to a data grid on the form.

The C# code for the XML web service is shown in Example 2-4.

Example 2-4. File: NorthwindServiceCS.asmx.cs

// Namespaces, variables, and constants
using System;
using System.ComponentModel;
using System.Web.Services;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public const String ORDERS_TABLE = "Orders";
public const String ORDERDETAILS_TABLE = "OrderDetails";

public const String ORDERID_FIELD = "OrderID";


// . . . 

public DataSet LoadOrders( )
 DataSet ds = new DataSet( );
 SqlDataAdapter da;

 // Fill the Order table and add it to the DataSet.
 da = new SqlDataAdapter("SELECT * FROM Orders",
 DataTable orderTable = new DataTable(ORDERS_TABLE);
 da.FillSchema(orderTable, SchemaType.Source);

 // Fill the OrderDetails table and add it to the DataSet.
 da = new SqlDataAdapter("SELECT * FROM [Order Details]",
 DataTable orderDetailTable = new DataTable(ORDERDETAILS_TABLE);
 da.FillSchema(orderDetailTable, SchemaType.Source);

 // Create a relation between the tables.

 return ds;

The C# web services client-side code is shown in Example 2-5.

Example 2-5. File: WebServiceDataSourceForm.cs

// Namespaces, variables, and constants
using System;
using System.Windows.Forms;
using System.Data;

// Table name constants
private const String ORDERS_TABLE= "Orders";

// . . . 

// Create the Web Service object.
NorthwindServiceCS nws = new NorthwindServiceCS( );
// Load the DataSet containing orders and order details.
DataSet ds = nws.LoadOrders( );

// Bind the default view of the orders table to the grid.
dataGrid.DataSource = ds.Tables[ORDERS_TABLE].DefaultView;


An XML web service is software that is accessible using Internet standards such as XML and HTTP. Because it is accessible through open -standard interfaces, web services make it easy to allow heterogeneous systems to work together.

.NET makes it very easy to build XML web services. In .NET, web services are implemented as .ASMX files beginning with a @WebService directive. For example, the solution code contains the following directive:

<%@ WebService Language="c#" Codebehind="NorthwindServiceCS.asmx.cs"
 Class="NorthwindServiceCS" %>

Methods in the web service class that are exposed over the Web are tagged with the WebMethod attribute; untagged methods can be used only internally by the web service. To deploy the web service, copy it to a virtual directory that has script execute permissions on an IIS web server that has ASP.NET support.

To use the web service class, use wsdl.exe to create the client-side proxy class. For the solution, the command is:

wsdl.exe http://localhost/NorthwindWebServiceCS/NorthwindServiceCS.asmx

Then, as with a local class, the client is able to instantiate the web service class using the new operator.

For more information about creating and consuming XML web services, see the MSDN Library.

The solution shows that there is very little difference between implementing the LoadOrders( ) methods to retrieve a DataSet containing the Orders and Order Details data from Northwind as a local class or as a web services class.

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax

ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton © 2008-2020.
If you may any questions please contact us: