Consuming Web Services

only for RuBoard

After a Web service is created and published on the Internet another entity, such as a business partner, client, or customer can use the Web service. This is referred to as consuming a Web service. The consumer can make a call to the Web service using any of the available protocols, Http-Get, Http-Post, or SOAP. The consumer of a Web service can be a Web application, a desktop application with Internet access, or another Web service.

To utilize a Web service, the consumer makes a proxy client, which provides programmatic access to all the exposed WebMethods in the Web service. The proxy client can be coded by hand, but it is much easier to use a utility to generate the proxy client. The .NET Framework SDK ships with a utility, named WSDL.exe, which is capable of generating proxy clients for Web services.

WSDL.exe

The WSDL.exe utility is a command-line utility used to generate proxy classes for consuming Web services. The WSDL.exe utility can generate proxy clients using any of the three protocols. The utility evaluates the WSDL provided by the Web service, and generates a proxy class in either Visual Basic, C#, or JScript. The proxy class then is compiled into a DLL and can be accessed by the consumer application. The proxy class provides methods for each of the WebMethods exposed by the Web service.

The WSDL.exe has a number of possible parameters, as seen in Table 14.1.

Table 14.1. Parameters of the WSDL.exe Utility
Parameter Description
/? Help. Entering wsd.exe /? at the command line will list all the parameters accepted by the WSDL utility.
/nologo Suppresses the banner.
/language:< language > Specifies the language used to write the proxy class. Accepted values are VB, CS, or JS. The default value is CS. /l : is the short form.
/server: Generates an abstract class for a Web service implementation based on the contracts. The default is to create a proxy class.
/namespace:< namespace > Specifies the namespace to assign to the proxy class. /n : is the short form.
/ out:< filename > Specifies the filename for the proxy class. The proxy class is created in the same directory in which the WSDL utility is run, unless a different path is defined.
/protocol:< protocol > Specifies the protocol the proxy client should use to call the Web service. Possible values are SOAP, Http-Get, or Http-Post. The default is SOAP.
/username:< username > Specifies the username to use when connecting to a server that requires authentication. /u : is the short form.
/password:< password > Specifies the password to use when connecting to a server that requires authentication. /p : is the short form.
/domain:< domain > The domain to use when connecting to a server that requires a domain name for authentication. /d : is the short form.

To create a client proxy class, you must know the URL of the WSDL document. The WSDL.exe utility takes in the URL and any specified parameters, and generates the proxy class. An example of a command to execute the WSDL utility is

 wsdl.exe http://localhost/myService.asmx?WSDL /l:VB /out:myProxyClient.vb /n:MyApp /p: graphics/ccc.gif HttpPost 

In the previous examples, you built a Web service for the Northwind application. You can create a proxy class to be used with the Northwind Web service. Included on the companion Web site for this book (http://www.samspublishing.com) is a sample client application, The Big Cheese, which is a customer of the Northwind Trading Company. The Big Cheese can consume the Northwind Web service and implement its own order processing by calling the Web service.

To create the structure for a new Web application for The Big Cheese, follows these steps:

  1. Create a directory on your computer named C:\Inetpub\ wwwroot \TheBigCheese .

  2. Create a subdirectory named \bin .

  3. Create another subdirectory named \ codebase .

  4. Copy the aspx, css, images, js, and usercontrols directories from the companion Web site into the \TheBigCheese directory on your computer.

  5. In the Internet Services Manager, create a new virtual directory named BigCheese , and point it to the \TheBigCheese directory.

After the Big Cheese Web application structure is in place, you can begin creating the proxy class.

Open a command line and enter the command in Listing 14.11.

Listing 14.11 Executing the WSDL.exe Utility
 [VB] wsdl http://localhost/northwind_vb/WebServices/ProductServices.asmx?WSDL /l:VB /n: graphics/ccc.gif Northwind.ProductServices /nologo /out:C: graphics/ccc.gif \Inetpub\wwwroot\TheBigCheese_VB\codebase\NorthwindProductServices_proxy.vb [C#] wsdl http://localhost/northwind_cs/WebServices/ProductServices.asmx?WSDL /l:CS /n: graphics/ccc.gif Northwind.ProductServices /nologo /out:C: graphics/ccc.gif \Inetpub\wwwroot\TheBigCheese_CS\codebase\NorthwindProductServices_proxy.cs 

The WSDL.exe utility will generate a proxy class in the \codebase directory of the \TheBigCheese directory. After the proxy client has been created, compile it into a DLL using a command-line compiler, as seen in Listing 14.12.

Listing 14.12 Compiling the Proxy Class with a Command-Line Compiler
 [VB] vbc /t:library /out:D:\TheBigCheese\bin\Northwind.ProductServices.dll C: graphics/ccc.gif \Inetput\wwwroot\TheBigCheese\codebase\NorthwindProductServices_proxy.vb /r: graphics/ccc.gif System.Web.Services.dll /r:System.dll /r:System.Xml.dll /r:System.Data.dll [C#] csc /t:library /out:D:\TheBigCheese\bin\Northwind.ProductServices.dll C: graphics/ccc.gif \Inetput\wwwroot\TheBigCheese\codebase\NorthwindProductServices_proxy.cs /r: graphics/ccc.gif System.Web.Services.dll /r:System.dll /r:System.Xml.dll /r:System.Data.dll 

Building the Consumer Web Form

A Web service consumer is an application that accesses a Web service. Using the WSDL.exe utility, you create a proxy client class that can be implemented to make calls to the exposed WebMethods of the Web service. The Big Cheese sample consumer application can be used to consume the Northwind ProductServices Web service. You can build a Web Form that calls the GetProducts() WebMethod . After the user has entered quantities for each product he wants to order, you can make a programmatic call to the PlaceOrder() WebMethod and the GetOrderDetail() WebMethod .

Listing 14.13 shows the code to build a consumer for the ProductServices() Web service. This code is for a Web Form (named BigCheese_Order.aspx ) that provides a user interface for placing an order, and calls the Web service to process the order. The Web Form built in Listing 14.13 belongs in the BigCheese directory.

Listing 14.13 Consuming the ProductServices Web Service
 [VB] 01: <%@ Page Language="VB" %> 02: <%@ Register TagPrefix="bigcheese" TagName="top" Src="user_controls/top.ascx" %> 03: <%@ Register TagPrefix="bigcheese" TagName="copyright" Src="user_controls/ graphics/ccc.gif copyright.ascx" %> 04: <%@ Import Namespace="System.Data" %> 05: <%@ Import Namespace="Northwind.ProductServices" %> 06: <script runat="server"> 07:  Protected Sub Page_Load(Sender As Object, E As EventArgs) 08:   If Not IsPostBack Then 09:    Dim dt As DateTime = DateTime.Today 10:    dt = dt.AddDays(7) 11:    Dim NWND_Products As Northwind.ProductServices.ProductServices = New graphics/ccc.gif Northwind.ProductServices.ProductServices() 12:    Dim getProducts As DataSet = NWND_Products.GetProducts(1) 13: 14:    RequiredDate.SelectedDate = dt 15:    RequiredDate.VisibleDate = dt 16: 17:    PlaceOrder.Visible = True 18:    FinalOrder.Visible = False 19:    Products.DataSource = getProducts.Tables("Products").DefaultView 20:    Products.DataBind() 21:   End If 22:  End Sub 23: 24:  Protected Sub Submit_Click(Sender As Object, E As EventArgs) 25:   Dim OrderID As Integer = 0 26: 27:   ' Create the DataSet to be passed to the Web Service 28:   Dim CustomerOrder As New DataSet 29: 30:   ' Add the required Order table to the DataSet 31:   CustomerOrder.Tables.Add("Order") 32: 33:   ' Add the ProductID and Quantity column to the Order table 34:   Dim ProductID As DataColumn = New DataColumn() 35:   ProductID.DataType = System.Type.GetType("System.Int32") 36:   ProductID.ColumnName = "ProductID" 37:   CustomerOrder.Tables("Order").Columns.Add(ProductID) 38: 39:   Dim Quantity As DataColumn = New  DataColumn() 40:   Quantity.DataType = System.Type.GetType("System.Int32") 41:   Quantity.ColumnName = "Quantity" 42:   CustomerOrder.Tables("Order").Columns.Add(Quantity) 43: 44:   ' Create the objects to be used in the For() loop 45:   Dim newRow As DataRow 46:   Dim ProductQuantity As TextBox 47:   Dim i As Integer = 0 48:   Dim newQnty As Integer 49:   Dim itemCount As Integer = 0 50: 51:   ' Any item with a quantity greater than 0 should be added to the order 52:   For i = 0 To Products.Items.Count-1 53:    ProductQuantity = Products.Items(i).FindControl("Qnty") 54:    newQnty = Integer.Parse(ProductQuantity.Text) 55: 56:    If newQnty > 0 Then 57:     newRow = CustomerOrder.Tables("Order").NewRow() 58:     newRow("ProductID") = Integer.Parse(Products.Items(i).Cells(0).Text) 59:     newRow("Quantity") = newQnty 60:     CustomerOrder.Tables("Order").Rows.Add(newRow) 61:     itemCount = itemCount + 1 62:    End If 63:   Next 64: 65:   ' Place the order by calling the Web Service if there are 1 or more items 66:   If itemCount > 0 Then 67:    Dim NWND_Products As Northwind.ProductServices.ProductServices = New graphics/ccc.gif Northwind.ProductServices.ProductServices() 68: 69:    ' "THEBI" is the CustomerID for The Big Cheese. 70:    ' This is the client page for Big Cheese, so the value is hard coded. 71:    OrderID = NWND_Products.PlaceOrder("THEBI", RequiredDate.SelectedDate, 1, graphics/ccc.gif ShipName.Text, ShipAddress.Text, ShipCity.Text, ShipRegion.Text, ShipPostalCode.Text, graphics/ccc.gif ShipCountry.Text, CustomerOrder) 72: 73:    ErrorMsg.Text = "Order Number: " & OrderID.ToString() 74: 75:    GetNewOrder(OrderID) 76:   Else 77:    ErrorMsg.Text = "<H5>Error: No quantities over zero (0) were entered. Your order graphics/ccc.gif was not placed.</H5>" 78:   End If 79:  End Sub 80: 81:  Protected Sub GetNewOrder(OrderID As Integer) 82:   Dim OrderInfo As DataSet 83:   Dim NWND_Products As Northwind.ProductServices.ProductServices = New graphics/ccc.gif Northwind.ProductServices.ProductServices() 84:   OrderInfo = NWND_Products.GetOrderDetail(OrderID) 85: 86:   Products.Visible = False 87:   Submit.Visible = False 88:   PlaceOrder.Visible = False 89:   FinalOrder.Visible = True 90: 91:   FinalOrder.DataSource = OrderInfo.Tables("OrderDetail").DefaultView 92:   FinalOrder.DataBind() 93:  End Sub 94: </script> [C#] 01: <%@ Page Language="C#" %> 02: <%@ Register TagPrefix="bigcheese" TagName="top" Src="user_controls/top.ascx" %> 03: <%@ Register TagPrefix="bigcheese" TagName="copyright" Src="user_controls/ graphics/ccc.gif copyright.ascx" %> 04: <%@ Import Namespace="System.Data" %> 05: <%@ Import Namespace="Northwind.ProductServices" %> 06: <script runat="server"> 07:  protected void Page_Load(Object sender, EventArgs e){ 08:   if(!IsPostBack){ 09:    DateTime dt = DateTime.Today; 10:    dt = dt.AddDays(7); 11:    Northwind.ProductServices.ProductServices NWND_Products = new graphics/ccc.gif Northwind.ProductServices.ProductServices(); 12:    DataSet getProducts = NWND_Products.GetProducts(1); 13: 14:    RequiredDate.SelectedDate = dt; 15:    RequiredDate.VisibleDate = dt; 16: 17:    PlaceOrder.Visible = true; 18:    FinalOrder.Visible = false; 19:    Products.DataSource = getProducts.Tables["Products"].DefaultView; 20:    Products.DataBind(); 21:   } 22:  } 23: 24:  protected void Submit_Click(Object sender, EventArgs e){ 25:   int OrderID = 0; 26: 27:   // Create the DataSet to be passed to the Web Service 28:   DataSet CustomerOrder = new DataSet(); 29: 30:   // Add the required Order table to the DataSet 31:   CustomerOrder.Tables.Add("Order"); 32: 33:   // Add the ProductID and Quantity column to the Order table 34:   DataColumn ProductID = new  DataColumn(); 35:   ProductID.DataType = System.Type.GetType("System.Int32"); 36:   ProductID.ColumnName = "ProductID"; 37:   CustomerOrder.Tables["Order"].Columns.Add(ProductID); 38: 39:   DataColumn Quantity = new  DataColumn(); 40:   Quantity.DataType = System.Type.GetType("System.Int32"); 41:   Quantity.ColumnName = "Quantity"; 42:   CustomerOrder.Tables["Order"].Columns.Add(Quantity); 43: 44:   // Create the objects to be used in the for() loop 45:   DataRow newRow; 46:   TextBox ProductQuantity; 47:   int i = 0; 48:   int newQnty; 49:   int itemCount = 0; 50: 51:   // Any item with a quantity greater than 0 should be added to the order 52:   for(i=0; i < Products.Items.Count-1; i++){ 53:    ProductQuantity = (TextBox)Products.Items[i].FindControl("Qnty"); 54:    newQnty = int.Parse(ProductQuantity.Text); 55: 56:    if(newQnty > 0){ 57:     newRow = CustomerOrder.Tables["Order"].NewRow(); 58:     newRow["ProductID"] = int.Parse(Products.Items[i].Cells[0].Text); 59:     newRow["Quantity"] = newQnty; 60:     CustomerOrder.Tables["Order"].Rows.Add(newRow); 61:     itemCount = itemCount + 1; 62:    } 63:   } 64: 65:   //Place the order by calling the Web Service if there are 1 or more items 66:   if(itemCount > 0){ 67:    Northwind.ProductServices.ProductServices NWND_Products = new graphics/ccc.gif Northwind.ProductServices.ProductServices(); 68: 69:    // "THEBI" is the CustomerID for The Big Cheese. 70:    // This is the client page for Big Cheese, so the value is hard coded. 71:    OrderID = NWND_Products.PlaceOrder("THEBI", RequiredDate.SelectedDate, 1, graphics/ccc.gif ShipName.Text, ShipAddress.Text, ShipCity.Text, ShipRegion.Text, ShipPostalCode.Text, graphics/ccc.gif ShipCountry.Text, CustomerOrder); 72: 73:    ErrorMsg.Text = "Order Number: " + OrderID.ToString(); 74: 75:    GetNewOrder(OrderID); 76:   } else{ 77:    ErrorMsg.Text = "<H5>Error: No quantities over zero (0) were entered. Your order graphics/ccc.gif was not placed.</H5>"; 78:   } 79:  } 80: 81:  protected void GetNewOrder(int OrderID){ 82:   DataSet OrderInfo; 83:   Northwind.ProductServices.ProductServices NWND_Products = new graphics/ccc.gif Northwind.ProductServices.ProductServices(); 84:   OrderInfo = NWND_Products.GetOrderDetail(OrderID); 85: 86:   Products.Visible = false; 87:   Submit.Visible = false; 88:   PlaceOrder.Visible = false; 89:   FinalOrder.Visible = true; 90: 91:   FinalOrder.DataSource = OrderInfo.Tables["NewOrder"].DefaultView; 92:   FinalOrder.DataBind(); 93:  } 94: </script> [VB & C#] 95: <html> 96: <body style="font: 8pt Verdana, Arial, sans-serif"> 97: <form runat="server" method="post"> 98: <bigcheese:top runat="server" Title="Beverage Order Form" /> 99: <asp:Label runat="server" ID="ErrorMsg" EnableViewState="False" 100:  ForeColor="Red" Font-Bold="True" Width="740"  /> 101: <table runat="server" ID="PlaceOrder" CellPadding="0" 102:  CellSpacing="0" Width="740"> 103:  <tr> 104:   <td> 105:    <table cellpadding="4" cellspacing="0"> 106:     <tr> 107:      <td style="font: 9pt Verdana, Arial, sans-serif"> 108:       <b>Ship To Name:</b> 109:      </td> 110:      <td> 111:       <asp:TextBox runat="server" id="ShipName" 112:        Text="The Big Cheese - Store #7" 113:        Width="300" Font-Name="Verdana" Font-Size="9pt" /> 114:      </td> 115:      <td style="font: 9pt Verdana, Arial, sans-serif"> 116:       <b>Date Required:</b> 117:      </td> 118:     </tr> 119:     <tr> 120:      <td style="font: 9pt Verdana, Arial, sans-serif"> 121:       <b>Ship To Address:</b> 122:      </td> 123:      <td> 124:       <asp:TextBox runat="server" id="ShipAddress" Text="123 Main Street" 125:        Width="300" Font-Name="Verdana" Font-Size="9pt" /> 126:      </td> 127:      <td rowspan="5"style="font: 9pt Verdana, Arial, sans-serif"> 128:       <asp:Calendar runat="server" ID="RequiredDate" 129:        TitleStyle-BackColor="Maroon" TitleStyle-ForeColor="White" 130:        TitleStyle-Font-Size="9pt" TitleStyle-Font-Bold="True" 131:        DayHeaderStyle-BackColor="Tan" DayHeaderStyle-ForeColor="Black" 132:        DayHeaderStyle-Font-Size="8pt" DayHeaderStyle-Font-Bold="True" 133:        DayStyle-BackColor="Tan" DayStyle-ForeColor="Black" 134:        DayStyle-Font-Size="8pt" 135:        SelectedDayStyle-BackColor="Maroon" 136:        SelectedDayStyle-ForeColor="White" 137:        Width="278" 138:       /> 139:      </td> 140:     </tr> 141:     <tr> 142:      <td style="font: 9pt Verdana, Arial, sans-serif"> 143:       <b>Ship To City:</b> 144:      </td> 145:      <td> 146:       <asp:TextBox runat="server" id="ShipCity" Text="Seattle" 147:        Width="300" Font-Name="Verdana" Font-Size="8pt" /> 148:      </td> 149:     </tr> 150:     <tr> 151:      <td style="font: 9pt Verdana, Arial, sans-serif"> 152:       <b>Ship To Region:</b> 153:      </td> 154:      <td> 155:       <asp:TextBox runat="server" id="ShipRegion" Text="WA" 156:        Width="300" Font-Name="Verdana" Font-Size="8pt" /> 157:       </td> 158:     </tr> 159:     <tr> 160:      <td style="font: 9pt Verdana, Arial, sans-serif"> 161:       <b>Ship To Postal Code:</b> 162:      </td> 163:      <td> 164:       <asp:TextBox runat="server" id="ShipPostalCode" Text="98101" 165:        Width="300" Font-Name="Verdana" Font-Size="8pt" /> 166:      </td> 167:     </tr> 168:     <tr> 169:      <td style="font: 9pt Verdana, Arial, sans-serif"> 170:       <b>Ship To Country:</b> 171:      </td> 172:      <td> 173:       <asp:TextBox runat="server" id="ShipCountry" Text="USA" 174:        Width="300" Font-Name="Verdana" Font-Size="8pt" /> 175:      </td> 176:     </tr> 177:    </table> 178:   </td> 179:  </tr> 180:  <tr> 181:   <td> 182:    <asp:DataGrid runat="server" ID="Products" AutoGenerateColumns="False" 183:     BorderWidth="1" BorderStyle="Solid" Gridlines="Both" BorderColor="Tan" 184:     Cellpadding="2" CellSpacing="0" Width="740" 185:     HeaderStyle-BackColor="Maroon" 186:     HeaderStyle-ForeColor="White" 187:     HeaderStyle-Font-Bold="True" 188:     HeaderStyle-Font-Name="Verdana" 189:     HeaderStyle-Font-Size="9pt" 190:     ItemStyle-Font-Name="Verdana" 191:     ItemStyle-Font-Size="8pt"> 192:     <Columns> 193:      <asp:BoundColumn DataField="ProductID" HeaderText="SKU" /> 194:      <asp:BoundColumn DataField="ProductName" HeaderText="Product Name" /> 195:      <asp:BoundColumn DataField="ProductName" HeaderText="Product Name" /> 196:      <asp:BoundColumn DataField="QuantityPerUnit" 197:       HeaderText="Quantity/Unit" /> 198:      <asp:BoundColumn DataField="UnitPrice" HeaderText="Unit Price" 199:       DataFormatString="{ 0:c} " ItemStyle-HorizontalAlign="Right" /> 200:      <asp:TemplateColumn HeaderText="Quantity" 201:       ItemStyle-HorizontalAlign="Center"> 202:       <ItemTemplate> 203:        <asp:TextBox runat="server" id="Qnty" Text="0" Width="50" 204:         Font-Size="8pt" Font-Name="Verdana" BorderStyle="Solid" 205:         BorderWidth="1" /> 206:       </ItemTemplate> 207:      </asp:TemplateColumn> 208:     </Columns> 209:    </asp:DataGrid> 210:   </td> 211:  </tr> 212:  <tr> 213:   <td align="right"> 214:    <asp:Button runat="server" ID="Submit" OnClick="Submit_Click" 215:     Text="Place Order" BorderStyle="Solid" BackColor="Maroon" 216:     ForeColor="White" Font-Bold="True" Font-Name="Verdana" 217:     Font-Size="9pt" style="cursor: hand;" /> 218:   </td> 219:  </tr> 220: </table> 221: <asp:DataGrid runat="server" ID="FinalOrder" AutoGenerateColumns="False" 222:  BorderWidth="1" BorderStyle="Solid" Gridlines="Both" BorderColor="Tan" 223:  Cellpadding="2" CellSpacing="0" Width="740" 224:  HeaderStyle-BackColor="Maroon" 225:  HeaderStyle-ForeColor="White" 226:  HeaderStyle-Font-Bold="True" 227:  HeaderStyle-Font-Name="Verdana" 228:  HeaderStyle-Font-Size="9pt" 229:  ItemStyle-Font-Name="Verdana" 230:  ItemStyle-Font-Size="8pt"> 231:  <Columns> 232:   <asp:BoundColumn DataField="ProductName" HeaderText="Product Name" /> 233:   <asp:BoundColumn DataField="UnitPrice" HeaderText="Unit Price" 234:    DataFormatString="{ 0:c} " ItemStyle-HorizontalAlign="Right" /> 235:   <asp:BoundColumn DataField="Quantity" HeaderText="Quantity" 236:    ItemStyle-HorizontalAlign="Center" /> 237:   <asp:BoundColumn DataField="Discount" HeaderText="Discount" 238:    ItemStyle-HorizontalAlign="Center" /> 239:   <asp:BoundColumn DataField="ExtendedPrice" HeaderText="Sub Total" 240:    DataFormatString="{ 0:c} " ItemStyle-HorizontalAlign="Right" /> 241:  </Columns> 242: </asp:DataGrid> 243: <bigcheese:copyright runat="server" /> 244: </form> 245: </body> 246: </html> 

In Listing 14.13, you build a consumer Web Form for the Northwind ProductServices Web service. The Web Form renders a series of TextBox es for inputting the shipping information (required by the PlaceOrder() WebMethod ). Each of the TextBox es has a default value. The Web Form also renders a Calendar control for setting the RequiredDate parameter required by PlaceOrder() . The Calendar 's default date is set to seven days from the current date. The Web Form also renders a DataGrid of products based on the GetProducts() WebMethod . The last column in the DataGrid is filled with TextBox es for entering the quantity of the product that is being ordered. Figure 14.9 shows the rendered Web Form before an order is placed.

Figure 14.9. The Web Form calls to the GetProducts() WebMethod in the Northwind ProductServices Web service. Server controls are rendered to input all the ordering information.
graphics/14fig09.gif

On lines 7 through 22 of Listing 14.13, you create the Page_Load() event handler. In the Page_Load() event on line 11, you create an instance of the Northwind.ProductServices proxy client class. On line 12, you create an instance of the DataSet class with the return value of the GetProducts() WebMethod . You pass the number 1 into the GetProducts() method, which is the value for beverages. The proxy class creates a SOAP message with the number 1 in the body of the message. The SOAP message is sent to the Web service, which calls the Products.GetProductsByCategory() method. The Web service returns a DataSet in a SOAP message to the Big Cheese consumer, and the getProducts DataSet is created. All of this is done without you having to create a SOAP message, or handle the return of a SOAP message. The page is rendered, and the beverages are bound to the Products DataGrid .

When the user clicks the Place Order button, the Submit_Click() event handler is called. In the Submit_Click() event handler, you programmatically create a new DataSet (line 28) and a new DataTable in the DataSet , named Order (line 31). On lines 33 through 42, you create two DataColumns in the Order DataTable , and assign a name and a data type to them. On lines 51 through 63, you loop through the DataGrid.Items collection and create a new DataRow for any item that has a quantity greater than 0. This is done by programmatically creating a TextBox and using the FindControl() method (line 53) to locate the TextBox in the last column of the DataGrid row. You assign it to the TextBox instance. On line 54, you use the int.Parse() [ Integer.Parse()] method to parse the Text property of the TextBox and set it to an integer variable. On lines 56 through 63, you add a new DataRow to the Order DataTable if the value in the TextBox is greater than 0. As you do this, you keep a running tally using the itemCount variable, on line 61, of how many products are being ordered (not their individual quantities). This tally is used to determine if the PlaceOrder() WebMethod should be called. If the Place Order button was clicked, even though no quantities were entered, the PlaceOrder() WebMethod will not be called, and an error message will be displayed to the user.

If the itemCount variable is greater than 0, then at least one product was ordered, and the PlaceOrder() WebMethod should be called. On line 67, you create a new instance of the ProductServices proxy client. You set the OrderID variable to the return value of the Place Order() WebMethod . You pass into the PlaceOrder() call the values from the TextBoxes , and the DataSet you programmatically created. Like the GetProducts() WebMethod , a SOAP message is built and sent to the Web service. The order is processed , and the order number is returned in a SOAP message, and assigned to the OrderID variable.

Using the OrderID variable, you call the GetOrderDetail() WebMethod . Again, a SOAP message is sent to the Web service. A SOAP message is returned, and a DataSet is created with the order detail. Figure 14.10 shows the Web Form after the order has been processed.

Figure 14.10. The user enters quantities for the order and clicks the Place Order button. The PlaceOrder() WebMethod is called, and an order number is returned.
graphics/14fig10.gif
only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

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