XML Web Services in Visual Basic .NET

 <  Day Day Up  >  

To demonstrate how you can add, edit, and delete records that are located on a computer on the other side of the Internet, I'm going to start with a form containing a grid that displays all of the records in a table. It's a simple design and eliminates the need to add a search form. However, you'll be able to see exactly how such a form could be built, using the techniques required for this example. Add, Edit, Delete and Close buttons at the bottom of the screen will provide the necessary functionality.

A Main Form for the Sample Application

My example form is shown in Figure 5.4.

Figure 5.4. The Main form for the Web service-based Visual Basic application.
graphics/05fig04.jpg

To build it, create a new Windows Application project called UseWS. Both the Solution and the Windows Application Project will be created wherever your default Visual Studio Projects directory is located, based on the default setting in your Tools, Options, Environment, Projects and Solutions default directory. Rename the form frmAllCustomers.vb . Add a DataGrid and four command buttons named cmdAdd , cmdEdit , cmdDelete , and cmdClose , with text captions to match, as shown in Figure 5.4. Double-click on the Close button and enter a single command: End . We'll code the other three buttons shortly.

When you use a local data source, you can add a data adapter and create a dataset from it. But when you're using Web Services, you need to create the Web service first, add a Web reference, and then base the dataset definition on the Web reference. So before going any further, we'll create the Web service.

Building a Web Service for the Sample Application

It's as easy to build a Web service in Visual Basic .NET as it is in Visual FoxPro 8. Add a new project of type Visual Basic ASP.NET Web Service to the solution. Name it Chapter5WebService. This will create a project file named Chapter5WebService.vbproj and a Web service file named Chapter5WebService.asmx . Asmx files aren't Web pages, so there are no visible controls to drop on them. However, data controls are used with XML Web Services, and they make data access very easy. Drag a SQLDataAdapter to the design surface. When the Data Adapter Wizard opens, select the Northwind connection that you created earlier. When asked to type a SELECT command, enter

 

 SELECT * FROM CUSTOMERS 

to return the entire Customer file. The wizard will generate a DataAdapter , which you can name daAllCustomers . Right-click on the DataAdapter and select Generate Dataset to create a typed dataset. Change the default name of Dataset1 to dsAllCustomers .

Drag a second DataAdapter to the Web Service design surface to provide a dataset for a single record. For the SQL statement, enter this:

 

 SELECT * FROM CUSTOMERS WHERE (CustomerID = @CustomerID) 

This creates a parameterized query, which will return one record. You can name the new DataAdapter daOneCustomer and the dataset dsOneCustomer . You'll be surprised how easy it is to send a parameter to a Web service and retrieve the selected record.

Changes to the CONFIG.WEB File

You'll need to open the Config.Web file in the project and add the following line immediately after the <system.web> tag:

 

 <identity impersonate="true" > 

This provides data requests access to the Web server without requiring a user login. While you're at it, enter the following immediately before the <system.web> tag:

 

 <appSettings>   <add key="ConnectionString" value="user id=sa;data source=localhost;persist security graphics/ccc.gif info=False;initial catalog=Northwind" /> </appSettings> 

This will allow us to read the connection string from a text file that can easily be changed when we deploy the application. We'll add that feature near the end of this topic.

Web Service Functions

Now you're ready to build our Web service. We'll need a function to populate the grid in the main application screen. That's what the dsAllCustomers dataset was for. When the user selects a record to edit, the second dataset, dsOneCustomer , will be used to return it. The Delete command button will just pass the key of the record to be deleted, to demonstrate the flexibility of Web services. They're called XML Web Services, but you don't have to pass XML back and forth. XML is just the transport mechanism for the parameters and results. You can send rows and columns back as XML, but scalars (strings, integers, dates, and the like) are just scalars.

Double-click on the design surface to open the code window, and enter the code shown in Listing 5.20.

Listing 5.20. Web Service Functions
 <WebMethod()> Public Function GetAllCustomers() _  As dsAllCustomers    Dim AllCustomers As New dsAllCustomers    SqlDataAdapter1.Fill(AllCustomers)    Return AllCustomers End Function <WebMethod()> Public Function GetOneCustomer( _   ByVal RecordKey As String) _  As dsOneCustomer    Dim OneCustomer As New dsOneCustomer    SqlDataAdapter2.SelectCommand.Parameters(0).Value = RecordKey    SqlDataAdapter2.Fill(OneCustomer)    Return OneCustomer End Function <WebMethod()> Public Function UpdateCustomers( _   ByVal customerChanges As dsOneCustomer) _  As dsOneCustomer    If Not (customerChanges Is Nothing) Then        SqlDataAdapter2.Update(customerChanges)        Return customerChanges    Else        Return Nothing    End If End Function <WebMethod()> Public Sub DeleteOneCustomer(ByVal RecordKey As String)    Dim cn As New SqlClient.SqlConnection    Cn.ConnectionString = ConfigurationSettings.AppSettings("ConnectionString")    cn.Open()    Dim sc As New SqlClient.SqlCommand("DELETE CUSTOMERS WHERE CustomerID='" + RecordKey + graphics/ccc.gif "'", cn)    sc.ExecuteNonQuery()    cn.Close() End Sub 

How It Works

AllCustomers returns all of the records in the Customers table of the Northwind database. For a few hundred records, performance will be adequate, as you'll see when you run this application.

The second line of the DeleteOneCustomer method shows you why I added the string to Web.Config a few paragraphs earlier. The program can read the Web.Config file and find and return the connection string stored in the file. That means you can change the server name, or even the database name, user ID or password, simply by editing a text file on the server.

The function declaration for GetOneCustomer says that it has a string parameter named RecordKey , which is passed to it. Its return value (the As clause at the end of the function declaration) is a dataset ” specifically , a dsOneCustomer dataset. So this function returns xml. So does AllCustomers because its return type is also a dataset.

UpdateCustomer accepts a single parameter of type dsOneCustomer ”a dataset ”and returns a dataset. It uses the dataset that is sent to it to call the data adapter's Update method, which applies the changes in the dataset (which is actually a DiffGram ) to the source table.

Finally, DeleteOneCustomer takes a string containing a CustomerID value, deletes one record, and returns nothing.

 <  Day Day Up  >  


Visual Fox Pro to Visual Basic.NET
Visual FoxPro to Visual Basic .NET
ISBN: 0672326493
EAN: 2147483647
Year: 2004
Pages: 130
Authors: Les Pinter

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net