4.11. Updating Server Data Using a Web Service
Problem
You want to update a data source using an XML web service and use the web service from your client application.
Solution
Use a DataSet object.
The XML web service code contains two methods:
LoadOrders( )Creates and returns a
DataSetcontaining the Orders and Order Details tables from Northwind and aDataRelationbetween those tables.UpdateOrders( )Takes a
DataSetargument containing the changes made to theDataSetcreated by theLoadOrders( )method, creates twoDataAdapterobjects withCommandBuildergenerated update logic for each, and uses theDataAdapterobjects to update the Orders and Order Details tables in Northwind.
The client-side code contains two event handlers:
Form.LoadSets up the example by calling the
LoadOrders( )method in the web service to populate aDataSet. The default view of the Orders table is bound to the data grid on the form.- Update
Button.Click Calls the
UpdateOrders( )method in the web service passing aDataSetcontaining changes made to theDataSetsince the form was loaded or since the last time theUpdateOrders( )method was called.
The C# code for the XML web service is shown in Example 4-25.
Example 4-25. 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access