15.4. Working with Web Service Methods That Return Data

Everything you have read in the preceding chapters on coding in Visual Studio .NET applies to creating Web services. In addition to the commands covered already, .NET provides classes to help you work with data. In comes ADO.NET.

15.4.1. Introducing ADO.NET

In Chapter 13, you were introduced to the concept of assemblies, namespaces, and classes. One of the namespaces that can be found in .NET is the System.Data namespace. This namespace makes up ADO.NET, which is a set of classes created to assist developers who work with data using the .NET Framework.

If you have done any development for the desktop using Visual Basic or VBA in the last couple of years, especially when dealing with data, then you have probably used ActiveX Data Objects ( ADO). ADO has an object model created for the purpose of manipulating data using code. On the .NET platform ADO.NET is used.

15.4.1.1. Differences between ADO and ADO.NET

The main difference between ADO and ADO.NET, besides specific objects, is the concept of connected data (ADO) versus disconnected data (ADO.NET). In both versions of ADO, you will use a Connection object, but in .NET the data is loaded either on the local computer or on the server using XML. The connection is then broken until the data is resubmitted to the source data. The ADO.NET objects, using properties, handle synchronization of data.

In ADO the main object was the Recordset, whereas in ADO.NET it is the DataSet. Unlike the Recordset from ADO and DAO, the DataSet actually brings back a hierarchical view of the data. Using properties and collections in the DataSet object, you can get from overall relations all the way down to individual tables, rows, and columns. You will read more about the DataSet object in the section in this chapter called "ADO.NET Objects."

Rather than going class by class through both ADO and ADO.NET, you will read about the classes used to work with the data in ADO.NET.

15.4.1.2. ADO.NET Data Provider Classes

.NET provides classes called data providers that will work with ADO.NET objects to provide access to data. You can see some of those objects in Figure 15-24.

Figure 15.24. Figure 15-24

As mentioned, Visual Studio .NET applications are made up of one or more assemblies. Each assembly contains one or more namespaces. Namespaces are then made up of one or more classes (objects). Hence the namespace for OleDb objects is System.Data.OleDb. You can find these objects using the Object Browser. In the following table you can see a brief description of two of the OleDb objects that you will use in creating the Web service method for supplying data.

ObjectPurpose
ConnectionThis object opens a connection to the server and database you want to work with. Unlike the ADO Connection object, how the connection remains open depends on the object you are working with, such as a DataReader or DataSet.
DataAdapterA real workhorse, the DataAdapter lets you create SQL statements and fill DataSets with the data. It also will create other action queries necessary such as Insert, Update, and Delete ADO.NET command objects.

15.4.1.3. ADO.NET Objects

As mentioned in the previous section, the main object used with ADO.NET is the DataSet object. You can see the DataSet object, and its properties, methods, and additional objects, in Figure 15-25.

The DataSet object is used in conjunction with the other data controls, storing the results that are returned by commands and DataAdapters.

Figure 15.25. Figure 15-25

15.4.1.4. Try It Out: Creating the Method to Return Supplier Information

For this Try It Out, you can replace the HelloWorld method created in the last Try It Out.

  1. Open the project just created.

  2. Add the following statement in the top of the *.cs file with the other using statements:

    using System.Data.OleDb;

  3. Add the code displayed here just after the last curly brace for the HelloWorld method:

    [WebMethod]
            public DataSet  GetSupplierInfoDS( long SupplierID )
            {
    
                     //Create a connection to the local Access Database.
                     string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0; ";
                     strCnn += "Data Source=" + Server.MapPath("/Chapter15WebService");
                     strCnn += "\Chapter 15.mdb";
    
                     OleDbConnection cnn = new OleDbConnection();
    
                     cnn.ConnectionString = strCnn;
    
                     //Create an SQL DataAdapter to read the data.
                     string strSQL = "SELECT SupplierID, CompanyName, ContactName, ";
                     strSQL += "ContactTitle, Address, City, Region, PostalCode, ";
                     strSQL += "Country, Phone, Fax FROM tblSuppliers ";
                     strSQL += "WHERE SupplierID=" + SupplierID;
    
                     OleDbDataAdapter daSuppliers = new
                                 OleDbDataAdapter(strSQL, cnn);

    // Create a DataSet and Fill it.
                     DataSet dsSuppliers = new DataSet();
                     daSuppliers.Fill( dsSuppliers);
    
                     return dsSuppliers;
    
            }

  4. Press F5 to rebuild and run the application. The introduction page to the test harness will display both HelloWorld and your new Web method.

  5. Click on the GetSupplierInfoDS link. You will then see the launch page for the Web method displayed with a text box to accept the Supplier ID parameter.

  6. Enter the parameter, as shown in Figure 15-26.

    Figure 15.26. Figure 15-26
  7. Click Invoke. The Web method is now executed, and the XML for the resulting data is displayed as shown in Figure 15-27.

Notice that the Web service returned not only the data, but also the schema for the data. This is what makes Web services so easy to use with InfoPath. Now you are ready to create other Web methods for various purposes.

For more information and practice with creating Web services for InfoPath check out the POWebService project, which is installed when you installed the Microsoft InfoPath 2003 SDK. This project displays more complicated methods for receiving and submitting data between InfoPath and Web services.

Figure 15.27. Figure 15-27

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.35.158