Sending a DataSet Using Web Services

for RuBoard

Sending a DataSet Using Web Services

To create a Web service, create a file in your Web project entitled suppliers.asmx. The .asmx extension tells the .NET Framework that this file should be treated as a Web service. Copy and paste the code from Listing 24.1 (or Listing 24.2 if you prefer C#) into the file and save. That's it! You've created your first Web service, which you can view in a browser by navigating directly to the suppliers.asmx URL.

Let's take a moment to the look at the Web service itself. Notice that the very first line of the file is a WebService directive. Up to this point, you have only seen a Page directive in that location. Notice also that the class of the Web service is provided in the WebService directive.

Next, several necessary namespaces are imported, just the same as in any Web form in this book. Then the Suppliers class is defined. The class inherits from the WebService class. The only difference is the WebMethod attribute specified before the publicly defined method. This specifies that the method is accessible as a Web method. The method is then defined. A DataSet is created and loaded with data from the Suppliers table in the Northwind database.

Listing 24.1 A Web Service That Returns All Suppliers in the Northwind Database in Visual Basic .NET
 <%@ WebService Language="VB" Class="Suppliers" %> Imports System.Web.Services Imports System.Data Imports System.Data.SqlClient Public Class Suppliers        Inherits WebService      <WebMethod ()> _      public function GetAllSuppliers() as DataSet            Dim conn as New SqlConnection("Initial Catalog=Northwind;" + _                                        "Server=(local);UID=sa;PWD=;")            Dim cmd as New SqlCommand("SELECT * FROM Suppliers", conn)            Dim adapter as SqlDataAdapter = New SqlDataAdapter(cmd)            Dim dsSuppliers as New DataSet()            conn.Open()            adapter.Fill(dsSuppliers, "Suppliers")            conn.Close()            Return dsSuppliers      End function End Class 
Listing 24.2 A Web Service That Returns All Suppliers in the Northwind Database in C#
 <%@ WebService Language="C#" Class="Suppliers" %> using System.Web.Services; using System.Data; using System.Data.SqlClient; public class Suppliers: WebService {      [ WebMethod() ]      public DataSet GetAllSuppliers()      {          SqlConnection conn = new SqlConnection("Initial Catalog=Northwind;" +                                      "Server=(local);UID=sa;PWD=;");          SqlCommand cmd = new SqlCommand("SELECT * FROM Suppliers", conn);          SqlDataAdapter adapter = new SqlDataAdapter(cmd);          DataSet dsSuppliers = new DataSet();          conn.Open();          adapter.Fill(dsSuppliers, "Suppliers");          conn.Close();          return dsSuppliers;      } } 

The GetAllSuppliers() method in Listings 24.1 and 24.2 is the same data access code that you've seen many times throughout this book. A DataSet of all the suppliers in the Northwind database is generated. The DataSet is then returned as the output of the function. Web services transport data using XML. Because DataSet s are represented internally as XML, you do not need to do anything else! The DataSet will automatically transmit.

When you navigate to the suppliers.asmx URL, you will see a screen like the one in Figure 24.1. It contains some information about Web services in general and shows you all the public methods supported by the current Web service. Note that in this case, the only public method is GetAllSuppliers() .

Figure 24.1. An automatically generated information screen for your Web service.


By clicking on the GetAllSuppliers() method, you will navigate to a screen like the one in Figure 24.2. This screen gives more detailed information about the GetAllSuppliers() method, including a sample SOAP request and response. These define exactly what type of data the Web service expects to receive and send in a call to this method.

Figure 24.2. Automatically generated information for calling your Web service.


If you click on the Invoke button to test the Web service, you will navigate to a screen like the one in Figure 24.3. This is the actual information returned by the Web service. If you look closely at the data, you'll see that it is the XML representation of a dataset. Because Web services transmit data packaged in XML, it's pretty handy that datasets are XML entities!

Figure 24.3. Invoking the GetAllSuppliers() Web service.


Now that you've created the Web service and ensured that it works by performing the tests, it's time to create a Web form that will use the DataSet returned from a remote machine to bind to a DataGrid .

What Is SOAP?


SOAP (Simple Object Access Protocol) is currently a hot topic in the development world, and for good reason. After all, it's the underlying protocol to Web services, which according to Microsoft is much of the drive behind its .NET strategy.

Generally speaking, SOAP is a protocol designed to facilitate the transfer of data, wrapped in XML over HTTP. SOAP defines the format of the Web service request, as seen at the bottom of Figure 24.1. In addition, SOAP defines the organization and format of the data returned, as you can see by invoking a Web service as in Figure 24.3.

Though it's always a good idea to understand how things work "under the hood," you don't need to understand SOAP to use Web services. The Microsoft .NET Framework handles all the details for you. Because SOAP is an XML-based (and thus human-readable ) protocol, you can examine the SOAP request and response in detail to find out what went wrong.

for RuBoard

Sams Teach Yourself ADO. NET in 24 Hours
Sams Teach Yourself ADO.NET in 24 Hours
ISBN: 0672323834
EAN: 2147483647
Year: 2002
Pages: 237 © 2008-2017.
If you may any questions please contact us: