Recipe 14.4. Creating a Web Service That Returns a Custom Object


Problem

You want to create a web service that returns a custom object because none of the .NET data types meets your needs.

Solution

Create a class that encapsulates the data you need and use it as the return type of a method of your web service.

To demonstrate this solution, we have created the custom class, BookData, shown in Examples 14-11 (VB) and 14-12 (C#). The class encapsulates information about books stored in a database. A class that uses a web service that returns book information from a database using the custom class is shown in Examples 14-13 (VB) and 14-14 (C#). Examples 14-15, 14-16 through 14-17 show the .aspx file and VB and C# code-behind files for our application that demonstrates how we use the web service. Figure 14-4 shows the Solution Explorer in Visual Studio 2005 with the files used for this recipe and other recipes in this chapter.

Figure 14-4. Solution Explorer showing files for this recipe


Discussion

Web services use XML to transfer data, and rely on the CLR to serialize most data types to XML. If you create an object that contains public properties or variables of the types the CLR can serialize, the CLR will serialize the object for you with no additional coding when it is used as the return type of a web service.

A custom object to be returned by a web service must meet two requirements. First, the object must contain only data types that can be serialized (they must implement the ISerializable interface). All of the .NET base data types and the majority of its complex data types can be serialized. The notable exceptions are DataTable (in ASP.NET 1.x), DataRow, and DataView.

Second, the class defining the object must include a public default constructor (one with no parameters). The CLR uses this constructor to serialize the object.

Let's take a look at the code we have written to illustrate this solution. To begin with, we have created the class shown in Examples 14-11 (VB) and 14-12 (C#), which encapsulates the data that a method of the web service will return. The class consists of the four sections shown in Table 14-1.

Table 14-1. Elements of the class returned by the sample web service

Element

Description

Private attributes

Used to store the object data

Public properties

Used to access the object data

First constructor

Used to create the object and populate it with data for a specific book

Second constructor

Provides the default public constructor required for serialization and provides the ability to create an object with all default values


The class shown in Examples 14-13 (VB) and 14-14 (C#) implements our web service. To make our example more useful, the getBookList method described in Recipe 14.1 is included in this class. For more on the getBookList web service method, refer to Recipe 14.1.

In our example, the getBookData method creates an instance of the BookData class and uses it as the return type for the method. The ID of the required book is passed to provide the constructor the information it needs to create the object and populate it with the requested book data:

 

bookInfo = New BookData(bookID) Return (bookInfo)

bookInfo = new BookData(bookID); return (bookInfo);

Our .aspx file, shown in Example 14-15, uses a ListBox to display a list of available books and a group of Literal controls to display the title, ISBN, and other details about the book.

The Page_Load method in our example's code-behind, shown in Examples 14-16 (VB) and 14-17 (C#), is responsible for populating the ListBox with the list of available books. Our first step is to create an instance of the web service proxy class:

 

bookServices = New ExampleBookServices.CH14BookServicesVB

bookServices = new ExampleBookServices.CH14BookServicesCS();

In our example, the web reference was renamed ExampleBookServices and the class that implements the web service is named CH14BookServicesVB (or CH14BookServicesCS).


Our next step is to call the getBookList method of the proxy class to get a list of available books:

 

books = bookServices.getBookList(NUMBER_OF_BOOKS)

books = bookServices.getBookList(NUMBER_OF_BOOKS);

After getting the list, the data is bound to the ListBox by setting the DataSource property to the DataSet containing the list of books. The DataTextField is set to the column in the DataSet containing the title of the book to define what will be displayed in the ListBox. The DataValueField is set to the column in the DataSet containing the BookID to identify the book when an item is selected. Finally, the DataBind method is called to bind the data in the DataSet to the ListBox:

 

lstBooks.DataSource = books lstBooks.DataTextField = "Title" lstBooks.DataValueField = "BookID" lstBooks.DataBind()

lstBooks.DataSource = books; lstBooks.DataTextField = "Title"; lstBooks.DataValueField = "BookID"; lstBooks.DataBind();

Rather than attempting to handle the case where the user does not select a book in the ListBox, we have simplified our example by selecting the first book in the list and then calling the getBookDetails method (described next):

 

lstBooks.SelectedIndex = 0 getBookDetails()

lstBooks.SelectedIndex = 0; getBookDetails();

The getBookDetails method is responsible for calling the web service that retrieves the details of the selected book. The getBookData method that is exposed by the web service is responsible for returning a custom object.

First, the method gets the ID of the selected book from the ListBox:

 

bookID = CInt(lstBooks.SelectedItem.Value)

bookID = System.Convert.ToInt32(lstBooks.SelectedItem.Value);

Next, an instance of the web service proxy class is created and the getBookData method is called to get the details of the book from the web service:

 

bookServices = New ExampleBookServices.CH14BookServicesVB bookInfo = bookServices.getBookData(bookID)

bookServices = new ExampleBookServices.CH14BookServicesCS(); bookInfo = bookServices.getBookData(bookID);

The controls used to display the book details are initialized with the data in the BookData object returned from the web service:

 

litTitle.Text = bookInfo.title litIsbn.Text = bookInfo.Isbn litDescription.Text = bookInfo.description litPublisher.Text = bookInfo.publisher litListPrice.Text = bookInfo.listPrice.ToString("0.00") litDate.Text = bookInfo.publishDate.ToShortDateString()

litTitle.Text = bookInfo.title; litIsbn.Text = bookInfo.Isbn; litDescription.Text = bookInfo.description; litPublisher.Text = bookInfo.publisher; litListPrice.Text = bookInfo.listPrice.ToString("0.00"); litDate.Text = bookInfo.publishDate.ToShortDateString();

One of the primary benefits of encapsulating data as we have in this example is the improvement in the performance of the web service. By returning the custom BookData object from our web service, all of the data is retrieved in a single call. If each piece of data for the book is obtained using separate calls, the application's performance will decrease. When you use a web service to retrieve data, return the largest possible block of data for each call. This reduces the significant overhead of serializing the data to XML and wrapping it with SOAP and HTTP protocol wrappers.

See Also

Recipe 14.1

.NET Web Service Idiosyncrasies

Web services produced and consumed by .NET applications can use any of the data types of the CLR that implement the ISerializable interface. If your web services need to be consumed by other technologies, such as Java, it will be necessary to limit the data types to simple types, such as integer, string, etc. In addition, .NET web services use Document Literal encoding by default, while many other technologies use RPC encoding. These factors must be considered when developing web services that are produced and consumed by different technologies.

Web services return data only

Web services let you pass data between applications; however, they cannot pass behavior. If you create a typical class that provides methods to act on the data in the instantiated object and use it as the return type from a web service method, you will find the methods to act on the data are unavailable when the object is returned. Though the object created by the web service and the object created by the application consuming the web service have the same name, they are not created from the same class. The BookData proxy class (VB version) created for our example is shown here and does not resemble the BookData class shown in Example 14-11. Essentially, it is a structure containing the data exposed by the class returned by the web service.

 <System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http:// www.dominiondigital.com")> _ Public Class BookData '<remarks/> Public title As String '<remarks/> Public Isbn As String '<remarks/> Public description As String '<remarks/> Public publisher As String '<remarks/> Public listPrice As Single '<remarks/> Public publishDate As Date '<remarks/> Public bookID As Integer End Class 


Example 14-11. Custom class (.vb)

 Option Explicit On Option Strict On Imports System Imports System.Configuration Imports System.Data Imports System.Data.OleDb Namespace VBWebServices ''' <summary> ''' This class provides the data class used to encapsulate book data ''' returned from a web service ''' </summary> Public Class BookData 'private attributes Private mBookID As Integer Private mTitle As String Private mIsbn As String Private mDescription As String Private mPublisher As String Private mListPrice As Single Private mPublishDate As DateTime '''*********************************************************************** ''' <summary> ''' This property provides the ability to get/set the book ID ''' </summary> Public Property bookID() As Integer Get Return (mBookID) End Get Set(ByVal Value As Integer) mBookID = Value End Set End Property 'bookID '''*********************************************************************** ''' <summary> ''' This property provides the ability to get/set the title ''' </summary> Public Property title() As String Get Return (mTitle) End Get Set(ByVal Value As String) mTitle = Value End Set End Property 'title '''*********************************************************************** ''' <summary> ''' This property provides the ability to get/set the ISBN ''' </summary> Public Property Isbn() As String Get Return (mIsbn) End Get Set(ByVal Value As String) mIsbn = Value End Set End Property 'Isbn '''*********************************************************************** ''' <summary> ''' This property provides the ability to get/set the description ''' </summary> Public Property description() As String Get Return (mDescription) End Get Set(ByVal Value As String) mDescription = Value End Set End Property 'description '''*********************************************************************** ''' <summary> ''' This property provides the ability to get/set the publisher ''' </summary> Public Property publisher() As String Get Return (mPublisher) End Get Set(ByVal Value As String) mPublisher = Value End Set End Property 'publisher '''*********************************************************************** ''' <summary> ''' This property provides the ability to get/set the listPrice ''' </summary> Public Property listPrice() As Single Get Return (mListPrice) End Get Set(ByVal Value As Single) mListPrice = Value End Set End Property 'listPrice '''*********************************************************************** ''' <summary> ''' This property provides the ability to get/set the publishDate ''' </summary> Public Property publishDate() As DateTime Get Return (mPublishDate) End Get Set(ByVal Value As Date) mPublishDate = Value End Set End Property 'publishDate '''*********************************************************************** ''' <summary> ''' This constructor creates the object and populates it with data for ''' the passed book ID. ''' </summary> ''' ''' <param name="ID">Set to the ID of the book from which to create the ''' object ''' </param> Public Sub New(ByVal ID As Integer) Dim dbConn As OleDbConnection = Nothing Dim da As OleDbDataAdapter = Nothing Dim dTable As DataTable = Nothing Dim dRow As DataRow = Nothing Dim connectionStr As String Dim cmdText As String Try 'get the connection string from web.config and open a connection 'to the database connectionStr = ConfigurationManager. _ ConnectionStrings("dbConnectionString").ConnectionString dbConn = New OleDbConnection(connectionStr) dbConn.Open() 'build the query string used to get the data from the database cmdText = "SELECT BookID, Title, ISBN, Description, Publisher, " & _   "ListPrice, PublishDate " & _   "FROM Book " & _   "WHERE BookBookID")) title = CStr(dRow.Item("Title")) Isbn = CStr(dRow.Item("ISBN")) description = CStr(dRow.Item("Description")) publisher = CStr(dRow.Item("Publisher")) listPrice = CSng(dRow.Item("ListPrice")) publishDate = CDate(dRow.Item("PublishDate")) Finally 'clean up If (Not IsNothing(dbConn)) Then dbConn.Close() End If End Try End Sub 'New '************************************************************************* ' ' ROUTINE: New ' ' DESCRIPTION: This constructor creates the object will default values '------------------------------------------------------------------------ Public Sub New() bookID = -1 title = "" Isbn = "" description = "" publisher = "" listPrice = 0 publishDate = DateTime.MinValue End Sub 'New End Class 'BookData End Namespace 

Example 14-12. Custom class (.cs)

 using System; using System.Configuration; using System.Data; using System.Data.OleDb; using System.Web; namespace CSWebServices { /// <summary> /// This class provides the data class used to encapsulate book data /// returned from a web service /// </summary> public class BookData { // private attributes private int mBookID; private string mTitle; private string mIsbn; private string mDescription; private string mPublisher; private Decimal mListPrice; private DateTime mPublishDate; ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the book ID /// </summary> public int bookID { get { return (mBookID); } set { mBookID = value; } } // bookID ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the title /// </summary> public string title { get { return (mTitle); } set { mTitle = value; } } // title ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the ISBN /// </summary> public string Isbn { get { return (mIsbn); } set { mIsbn = value; } } // Isbn ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the description /// </summary> public string description { get { return (mDescription); } set { mDescription = value; } } // description ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the publisher /// </summary> public string publisher { get { return (mPublisher); } set { mPublisher = value; } } // publisher ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the listPrice /// </summary> public Decimal listPrice { get { return (mListPrice); } set { mListPrice = value; } } // listPrice ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the publishDate /// </summary> public DateTime publishDate { get { return (mPublishDate); } set { mPublishDate = value; } } // publishDate ///*********************************************************************** /// <summary> /// This constructor creates the object and populates it with data for /// the passed book ID. /// </summary> /// /// <param name="ID">Set to the ID of the book from which to create the /// object /// </param> public BookData(int ID) { OleDbConnection dbConn = null; OleDbDataAdapter da = null; DataTable dTable = null; DataRow dRow = null; string connectionStr = null; string cmdText = null; try { // get the connection string from web.config and open a connection // to the database connectionStr = ConfigurationManager. ConnectionStrings["dbConnectionString"].ConnectionString; dbConn = new OleDbConnection(connectionStr); dbConn.Open(); //build the query string used to get the data from the database cmdText = "SELECT BookID, Title, ISBN, Description, Publisher, " +   "ListPrice, PublishDate " +   "FROM Book " +   "WHERE BookBookID"]); title = (string)(dRow["Title"]); Isbn = (string)(dRow["ISBN"]); description = (string)(dRow["Description"]); publisher = (string)(dRow["Publisher"]); listPrice = (Decimal)(dRow["ListPrice"]); publishDate = (DateTime)(dRow["PublishDate"]); } finally { // cleanup if (dbConn != null) { dbConn.Close(); } } // finally } //************************************************************************ // // ROUTINE: BookData // // DESCRIPTION: This constructor creates the object will default values //----------------------------------------------------------------------- public BookData() { bookID = -1; title = ""; Isbn = ""; description = ""; publisher = ""; listPrice = 0; publishDate = DateTime.MinValue; } } // BookData } 

Example 14-13. Web service returning a custom object (.vb)

 Option Explicit On Option Strict On Imports System.Configuration Imports System.Data Imports System.Data.OleDb Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols Namespace VBWebServices ''' <summary> ''' This class provides the code-behind for CH14BookServicesVB.asmx ''' </summary> <WebService(Namespace:="http://www.dominiondigital.com/")> _ <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ Public Class CH14BookServicesVB Inherits System.Web.Services.WebService '''*********************************************************************** ''' <summary> ''' This routine gets the list of books from the database. ''' </summary> ''' ''' <param name="numberOfBooks">Set to the number of books to retrieve ''' </param> ''' <returns>DataSet containing the list of books</returns> <WebMethod()> _ Function getBookList(ByVal numberOfBooks As Integer) As DataSet Dim dbConn As OleDbConnection = Nothing Dim da As OleDbDataAdapter = Nothing Dim dSet As DataSet = Nothing Dim connectionStr As String Dim cmdText As String Try 'get the connection string from web.config and open a connection 'to the database connectionStr = ConfigurationManager. _ ConnectionStrings("dbConnectionString").ConnectionString dbConn = New OleDbConnection(connectionStr) dbConn.Open() 'build the query string used to get the data from the database cmdText = "SELECT Top " & numberOfBooks.ToString() & " " & _   "BookID, Title, ISBN, Publisher " & _   "FROM Book " & _   "ORDER BY Title" 'create a new dataset and fill it with the book data dSet = New DataSet da = New OleDbDataAdapter(cmdText, dbConn) da.Fill(dSet) 'return the list of books Return (dSet) Finally 'clean up If (Not IsNothing(dbConn)) Then dbConn.Close() End If End Try End Function 'getBookList '''*********************************************************************** ''' <summary> ''' This routine gets the data for the passed book ''' </summary> ''' ''' <param name="bookID">Set to the ID of the book for which the data is ''' required ''' </param> ''' <returns>BookData containing the data for the requested book</returns> <WebMethod()> _ Function getBookData(ByVal bookID As Integer) As BookData Dim bookInfo As BookData 'create a new BookData object containing the requested data bookInfo = New BookData(bookID) Return (bookInfo) End Function ' getBookData End Class 'CH14BookServicesVB End Namespace 

Example 14-14. Web service returning a custom object (.cs)

 using System; using System.Configuration; using System.Data; using System.Data.OleDb; using System.Web; using System.Web.Services; namespace CSWebServices {  /// <summary>  /// This module provides the code behind for the CH14BookServicesCS.asmx  /// </summary>  [WebService(Namespace = "http://www.dominiondigital.com/")]  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  public class CH14BookServicesCS : System.Web.Services.WebService  { ///*********************************************************************** /// <summary> /// This routine gets the list of books from the database. /// </summary> /// /// <param name="numberOfBooks">Set to the number of books to retrieve /// </param> /// <returns>DataSet containing the list of books</returns> [WebMethod] public DataSet getBookList(int numberOfBooks) { OleDbConnection dbConn = null; OleDbDataAdapter da = null; DataSet dSet = null; String connectionStr = null; String cmdText = null; try { // get the connection string from web.config and open a connection // to the database connectionStr = ConfigurationManager. ConnectionStrings["dbConnectionString"].ConnectionString; dbConn = new OleDbConnection(connectionStr); dbConn.Open(); //build the query string used to get the data from the database cmdText = "SELECT Top " + numberOfBooks.ToString() + " " +   "BookID, Title, ISBN, Publisher " +   "FROM Book " +   "ORDER BY Title"; // create a new dataset and fill it with the book data dSet = new DataSet(); da = new OleDbDataAdapter(cmdText, dbConn); da.Fill(dSet); //return the list of books return (dSet); } // try finally { // cleanup if (dbConn != null) { dbConn.Close(); } } // finally } // getBookList ///*********************************************************************** /// <summary> /// This routine gets the data for the passed book /// </summary> /// /// <param name="bookID">Set to the ID of the book for which the data is /// required /// </param> /// <returns>BookData containing the data for the requested book</returns> [WebMethod] public BookData getBookData(int bookID) { BookData bookInfo = null; // create a new BookData object containing the requested data bookInfo = new BookData(bookID); return (bookInfo); } // getBookData } // CH14BookServicesCS } 

Example 14-15. Using the web service returning a custom object (.aspx)

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" AutoEventWireup="false" CodeFile="CH14CustomObjectWithWebServiceVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH14CustomObjectWithWebServiceVB" Title="Web Service Returning A Custom Object" %> <asp:Content  runat="server" ContentPlaceHolder> <div align="center" > Returning a Custom Object From a Web Service (VB) </div> <table width="90%" align="center" border="0"> <tr> <td align="center" > <br />Available Books<br /><br /> <asp:ListBox  Runat="server" AutoPostBack="True" OnSelectedIndexChanged="lstBooks_SelectedIndexChanged" /> </td> </tr> <tr> <td> <br /> <table width="60%" align="center"   border="1"   cellpadding="5" cellspacing="0" > <tr> <td width="25%" align="right">Title: </td> <td width="75%"> <asp:Literal  Runat="server" /></td> </tr> <tr> <td width="25%" align="right">ISBN: </td> <td width="75%"> <asp:Literal  Runat="server" /></td> </tr> <tr> <td width="25%" align="right">Description: </td> <td width="75%"> <asp:Literal  Runat="server" /></td> </tr> <tr> <td width="25%" align="right">Publisher: </td> <td width="75%"> <asp:Literal  Runat="server" /></td> </tr> <tr> <td width="25%" align="right">List Price: </td> <td width="75%"> <asp:Literal  Runat="server" /></td> </tr> <tr> <td width="25%" align="right">Date: </td> <td width="75%"> <asp:Literal  Runat="server" /></td> </tr> </table> </td> </tr>   </table> </asp:Content> 

Example 14-16. Using the web service returning a custom object code-behind (.vb)

 Option Explicit On Option Strict On Imports System Imports System.Data Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides the code behind for ''' CH14CustomObjectWithWebServiceVB.aspx ''' </summary> Partial Class CH14CustomObjectWithWebServiceVB Inherits System.Web.UI.Page '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the page load event. It ''' is responsible for initializing the controls on the page. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Private Sub Page_Load(ByVal sender As Object, _   ByVal e As System.EventArgs) Handles Me.Load Const NUMBER_OF_BOOKS As Integer = 20 Dim bookServices As ExampleBookServices.CH14BookServicesVB Dim books As DataSet If (Not Page.IsPostBack) Then 'create an instance of the web service proxy class bookServices = New ExampleBookServices.CH14BookServicesVB 'get the books from the service books = bookServices.getBookList(NUMBER_OF_BOOKS) 'bind the book list to the listbox on the form lstBooks.DataSource = books lstBooks.DataTextField = "Title" lstBooks.DataValueField = "BookID" lstBooks.DataBind() 'select the first item in the list and get the details lstBooks.SelectedIndex = 0 getBookDetails() End If End Sub 'Page_Load '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the book listbox selected ''' index changed event. It is responsible for getting the book data ''' for the selected book ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Protected Sub lstBooks_SelectedIndexChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) 'get the data for the selected book  getBookDetails() End Sub 'lstBooks_SelectedIndexChanged '''*********************************************************************** ''' <summary> ''' This routine gets the details for the currently selected book and ''' initializes the controls on the with the data. ''' </summary> Private Sub getBookDetails() Dim bookServices As ExampleBookServices.CH14BookServicesVB Dim bookInfo As ExampleBookServices.BookData Dim bookID As Integer 'get the currently selected book bookID = CInt(lstBooks.SelectedItem.Value) 'create an instance of the web service proxy class 'and get the book data bookServices = New ExampleBookServices.CH14BookServicesVB bookInfo = bookServices.getBookData(bookID) 'set the controls on the form to display the book data litTitle.Text = bookInfo.title litIsbn.Text = bookInfo.Isbn litDescription.Text = bookInfo.description litPublisher.Text = bookInfo.publisher litListPrice.Text = bookInfo.listPrice.ToString("0.00") litDate.Text = bookInfo.publishDate.ToShortDateString() End Sub 'getBookDetails End Class 'CH14CustomObjectWithWebServiceVB End Namespace 

Example 14-17. Using the web service returning a custom object code-behind (.cs)

 using System; using System.Data; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides the code behind for /// CH14CustomObjectWithWebServiceCS.aspx /// </summary> public partial class CH14CustomObjectWithWebServiceCS : System.Web.UI.Page { ///*********************************************************************** /// <summary> /// This routine provides the event handler for the page load event. /// It is responsible for initializing the controls on the page. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void Page_Load(object sender, EventArgs e) { const int NUMBER_OF_BOOKS = 20; ExampleBookServices.CH14BookServicesCS bookServices = null; DataSet books = null; if (!Page.IsPostBack) { // create an instance of the web service proxy class bookServices = new ExampleBookServices.CH14BookServicesCS(); // get the books from the service books = bookServices.getBookList(NUMBER_OF_BOOKS); // bind the book list to the listbox on the form lstBooks.DataSource = books; lstBooks.DataTextField = "Title"; lstBooks.DataValueField = "BookID"; lstBooks.DataBind(); // select the first item in the list and get the details lstBooks.SelectedIndex = 0; getBookDetails(); } } // Page_Load ///*********************************************************************** /// <summary> /// This routine provides the event handler for the book listbox selected /// index changed event. It is responsible for getting the book data /// for the selected book /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void lstBooks_SelectedIndexChanged(Object sender,   System.EventArgs e) { // get the data for the selected book getBookDetails(); } // lstBooks_SelectedIndexChanged ///*********************************************************************** /// <summary> /// This routine gets the details for the currently selected book and /// initializes the controls on the with the data. /// </summary> private void getBookDetails() { ExampleBookServices.CH14BookServicesCS bookServices = null; ExampleBookServices.BookData bookInfo = null; int bookID; // get the currently selected book bookID = System.Convert.ToInt32(lstBooks.SelectedItem.Value); // create an instance of the web service proxy class // and get the book data bookServices = new ExampleBookServices.CH14BookServicesCS(); bookInfo = bookServices.getBookData(bookID); // set the controls on the form to display the book data litTitle.Text = bookInfo.title; litIsbn.Text = bookInfo.Isbn; litDescription.Text = bookInfo.description; litPublisher.Text = bookInfo.publisher; litListPrice.Text = bookInfo.listPrice.ToString("0.00"); litDate.Text = bookInfo.publishDate.ToShortDateString(); } // getBookDetails } // CH14CustomObjectWithWebServiceCS } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

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