This documentation is archived and is not being maintained.

DataSetConsumer Sample: Consumes Multiple Table Datasets 

Download sample

Demonstrates how to consume multiple table datasets from an unmanaged Visual C++ client using an XML Web service proxy.

The DataSetConsumer sample illustrates how an unmanaged Web application can consume data from a managed XML Web service. DataSetConsumer is an ATL Server application that consumes a multiple table dataset exposed through an XML Web service, WebService1, which you create using ASP.NET.

DataSetConsumer also shows how to use Web references to support the consumption of multiple table datasets. Web references convert single-table datasets into C++ structures but treat multiple-table datasets as XML streams. Therefore, the corresponding XML Web service method in the proxy class returns the full XML serialization of the dataset as string data. It must then be parsed to be useful in most applications.

Security noteSecurity Note

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

Requirements

  • IIS must be installed and running.

  • At least one SQL Server database must be installed.

Demonstrates

Building and Running the Sample

To use this sample, follow the steps in this section. You will create an XML Web service project WebService1 that defines a Web method to access data from a dataset. You then add a Web reference, which creates a proxy class for the XML Web service. DataSetConsumer uses an instance of this proxy to call the XML Web service method in WebService1. WebService1 returns the dataset as type BSTR, and DataSetConsumer loads it as XML, using the DOM and the MSXML parser to parse the string and print the data as HTML.

To create an ASP.NET Web service project to access a dataset

  1. Create a new XML Web service using ASP.NET.

    On the File menu, click New, and then click Project. The New Project dialog box appears. Click Visual C# Projects in the Project Types pane, and then click ASP.NET Web Service in the Templates pane. In the Name box, enter WebService1. Click OK.

    NoteNote

    Note   The location should be the default location (http://localhost/WebService1).

    When the project has been created, you will see Service1.asmx.cs in the Design pane.

  2. Create a data connection.

    In Server Explorer, right-click Data Connection. On the shortcut menu, click Add Connection. The Data Link Properties dialog box appears. Complete it to create a data connection to a database, for example, the Northwind database.

  3. Add tables and data adapters.

    Open the Tables node of your new data connection and drag two tables from Server Explorer to the Design pane. Two DataAdapters and one data connection will appear in the Design pane.

  4. Generate a dataset.

    On the Data menu, click Generate Dataset. The Generate Dataset dialog box appears. Choose a New Dataset named DataSet1 (the default), and add both tables to the dataset. Also select the Add this dataset to the designer box (optional but recommended). Click OK to create the dataset (named dataSet1 by default).

  5. Implement a Web method.

    Right-click on the Service1.asmx.cs design surface in the Design pane. On the shortcut menu, click View Code. The code for Service1.asmx.cs appears in the Edit pane. Scroll to the bottom of the file to the HelloWorld example Web method:

    //      [WebMethod]
    //      public string HelloWorld()
    //      {
    //         return "Hello World";
    //      }
    

    In place of the HelloWorld example code, implement a new Web method named GetDataSet as follows:

          [WebMethod]
          public DataSet1 GetDataSet()
          {
             this.sqlDataAdapter1.Fill (this.dataSet11);
             return dataSet11;
          }
    
    NoteNote

    This code assumes that you use a SQL Server database.

  6. Build the WebService1 project. On the Build menu, click Build Solution.

To consume data from DataSetConsumer

  1. Open the DataSetConsumer project.

    Open the solution file, DataSetConsumer.sln, in the Visual Studio development environment.

    Make sure that DataSetConsumer.h contains this line:

    #include "Service1.h"
    
  2. Add a Web reference to the project.

    Adding a Web reference will generate a proxy class for the XML Web service. An XML Web service client can call a method on the proxy, which in turn does the necessary work to remotely invoke the call over the network to call the corresponding XML Web service method. By default, the proxy class uses SOAP to access the corresponding method, because SOAP supports the richest set of data types of the three supported protocols (SOAP, HTTP-GET, and HTTP-POST). Adding a Web reference can generate proxy classes for the latter two protocols as well.

    In Solution Explorer, right-click the DataSetConsumer project node. On the shortcut menu, click Add Web Reference.

    The Add Web Reference dialog box appears.

    In the left pane of the Add Web Reference dialog box, click Web References on Local Machine and wait for the wizard to download the XML Web service files. When the files are finished downloading, a link and URL for each registered XML Web service will appear in the left pane. Click the link for the Service1 Web service with the URL:

    http://localhost/WebService1/Service1.asmx
    

    The contents of the XML Web service's description file appears in the left pane. The Web service description is an XML document written in an XML grammar called Web Service Description Language (WSDL) that defines the format of messages for the XML Web service.

    In the Web reference name box, enter Service1.

    Click Add Reference. This adds the proxy class Service1 to the DataSetConsumer project. The header file Service1.h appears in Solution Explorer.

    NoteNote

    In Service1.h, the CService1T constructor is hardcoded to use the URL specified when you added the Web reference. If you want to use DataSetConsumer with another XML Web service, change the URL to match the location of that XML Web service's .asmx file.

  3. Build the DataSetConsumer project. On the Build menu, click Build Solution.

  4. Run the client application.

    On the Debug menu, click Start Without Debugging.

    When prompted for a URL, enter the following URL:

    http://localhost/DataSetConsumer/DataSetConsumer.srf
    

    Click OK. The Web browser appears, displaying the Northwind data accessed through the XML Web service.

See Also

Show: