HOW TO: Update Server Data Through a Web Service by Using ADO.NET and Visual J# .NET (320634)
The information in this article applies to:
- Microsoft ADO.NET (included with the .NET Framework 1.1)
- Microsoft ADO.NET (included with the .NET Framework) 1.0
- Microsoft ASP.NET (included with the .NET Framework 1.1)
- Microsoft ASP.NET (included with the .NET Framework) 1.0
- Microsoft Visual J# .NET (2003)
- Microsoft Visual J# .NET (2002)
This article was previously published under Q320634 For a Microsoft Visual Basic .NET version of this
article, see
308056. For a Microsoft Visual C#
.NET version of this article, see
310143. This article refers to the following Microsoft .NET
Framework Class Library namespace:
IN THIS TASKSUMMARY This step-by-step article demonstrates how to use an Web
service to receive and to update data from a database by using a DataSet object. This article also demonstrates how to reference the Web
service in a client application and how to display the returned DataSet in a DataGrid control so that you can update that data and send the updates
back to the server. NOTE: You can only use the method in this article for single-table
updates.
back to the top
Requirements The following list outlines the recommended hardware, software,
network infrastructure, and service packs that are required:
- Microsoft Windows 2000 Professional, Windows 2000 Server,
Windows 2000 Advanced Server, or Windows NT 4.0 Server
- Microsoft Visual Studio .NET
- Microsoft Visual J# .NET
This article assumes that you are familiar with the following
topics:
- Visual Studio .NET
- ADO.NET fundamentals and syntax
- ASP.NET fundamentals and syntax
The code samples in this article use http://localhost as the
Web server. Additionally, the code samples use the Northwind database. The Northwind database is included with Microsoft SQL Server.
back to the top
Create the Web Service- Start Visual Studio .NET.
- Follow these steps to create a new Visual J# ASP.NET Web
Service project:
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, click Visual J# Projects under Project Types, and then click ASP.NET Web Service under Templates.
- In the Location box, type the URL for your server and the project name,
jsUpdateData (for example, http://localhost/jsUpdateData). The http://localhost
portion of the URL runs the Web service on your local Web server. Click OK.
- On the Service1.asmx.jsl[Design] tab, right-click the page, and then click View Code to switch to Code view. The Code window for the Web service
appears.
- At the top of the Code window, after the package statement,
add the following import statement:
//Use data access objects from the SqlClient namespace.
import System.Data.SqlClient.*;
- After the following code
public Service1()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
add the following code:
/** @attribute WebMethod() */
public DataSet GetCustomers()
{
SqlConnection con = new SqlConnection("server=servername;uid=login;pwd=password;database=northwind");
SqlDataAdapter daCust = new SqlDataAdapter("Select * From Customers", con);
DataSet ds = new DataSet();
daCust.Fill(ds, "Cust");
return ds;
}
/** @attribute WebMethod() */
public DataSet UpdateCustomers(DataSet ds)
{
SqlConnection con = new SqlConnection("server=servername;uid=login;pwd=password;database=northwind");
SqlDataAdapter daCust = new SqlDataAdapter("Select * From Customers", con);
SqlCommandBuilder cbCust = new SqlCommandBuilder(daCust);
daCust.Update(ds, "Cust");
return ds;
}
- Modify the SqlConnection string to properly connect to the computer that is running SQL
Server.
back to the top
Test the Web Service- Press F5 to compile and to run the Web service. A Web page
is displayed in which you can interact with the Web service from within
Microsoft Internet Explorer.
Note that the URL of the returned page
is http://localhost/jsUpdateData/Service1.asmx. - On the Service1 Web page, click GetCustomers. A Web page is displayed that includes details about the GetCustomers Web method.
- Close the Web pages.
back to the top
Create the Client Application- In Visual Studio .NET, create a new Visual J# Windows
Application project. By default, Form1 is added to the project.
- Add two Button controls and one DataGrid control to Form1. By default, Button1, Button2, and DataGrid1 are added to the project.
- Change the Text property of Button1 to Load, and then change the Text property of Button2 to Save.
- On the Project menu, click Add Web Reference. Type the URL for your Web service (for example, type
http://localhost/jsUpdateData/Service1.asmx), press
ENTER, and then click Add Reference. The entry for the newly added Web reference appears in Solution
Explorer.
- In the Visual J# project, double-click Load to open the Code window for Load. Add the following code to the Load_Click event procedure:
localhost.Service1 MyService = new localhost.Service1();
dataGrid1.set_DataSource(MyService.GetCustomers());
dataGrid1.set_DataMember("Cust");
- Switch to Form view.
- Double-click Save to open the Code window for Save. Add the following code into the Save_Click event procedure:
localhost.Service1 MyService = new localhost.Service1();
DataSet ds = (DataSet) dataGrid1.get_DataSource();
DataSet dsChanges = ds.GetChanges();
if (dsChanges != null)
{
ds.Merge(MyService.UpdateCustomers(dsChanges), true);
}
back to the top
Test the Client Application- Press F5 to compile and to run the client
application.
- Notice that initially DataGrid1 is empty. Click Load. Note that DataGrid1 now displays the Customer records.
- In DataGrid1, modify some of the data, and then click Save.
NOTE: Do not change the key field. If you change the key field, you
receive an error message, which states that you are breaking referential
integrity on the server.
back to the top
REFERENCES For more information, see the "Creating and Accessing XML
Web Services Walkthroughs" topic in the Visual Studio .NET Help
documentation.
back to the top
Modification Type: | Major | Last Reviewed: | 9/3/2003 |
---|
Keywords: | kbHOWTOmaster kbSqlClient kbSystemData KB320634 kbAudDeveloper |
---|
|