Creating an ADO.NET Shopping Cart • Chapter 12 |
551 |
}
The method catalogRange will return all data for items in a given range:
public DataSet catalogRange(int startPos, int range)
{//returns a given range of books
return catalogRangeByCategory( startPos, range, -1, null);
}
The method catalogByCategory will return all data for items in a given category:
public DataSet catalogByCategory( int catId)
{ //returns all books with the given categoryId
return catalogRangeByCategory( -1, -1, catId, null);
}
Creating the catalogRangeByCategory Method
The catalogRangeByCategory method creates a new DataSet containing a new Books Table, appends the appropriate Summary Table, and returns this new DataSet. It is used by the preceding methods to return a single item’s node (to add to the shopping cart), to return a range of books (to handle browsing the catalog), and to return all books in a given category (to handle viewing by category). A method could easily be added that enables browsing by category.
In order to return a subset of the DataSet allBooks, we need to create a new DataTable object that has the same table structure as Books.We can then import rows that meet our criteria into this new table.When the table is filled, we create a new DataSet object and add the new DataTable as well as a Summary Table.
The resulting DataSet will contain the request subset of data and some Metainformation (supplied by the Summary table).
Now, let’s examine the code. Create a temporary DataTable that holds allBooks data:
DataTable dtTemp = dsAllBooks.Tables["Books"];
Clone the structure of this table in a new DataTable:
DataTable dtBooks = dtTemp.Clone();//create Empty Books Table
Set the filter expression property based on input parameters:
if( catId == -1)
{ //no filter is applied strExpr = "";