Using Typed DataSets

The Web Store uses untyped datasets for data access, but if you choose you can also easily use typed dataset. The process to do so involves two steps:

  1. Set up your business object's DataSet object
  2. Cast the DataSet on results to your DataSet

The first step involves setting the DataSet object in the constructor of the business object subclass you create and assigning it an instance of your custom typed (or even a custom untyped) DataSet subclass:

public busCustomer()
{
	this.Tablename = "wws_customers";
	this.ConnectionString = "server=(local);database=WebStore;integrated security=true;";
	this.NewRowBlankValues = true;

	// *** Assign customer Typed DataSet
	<<b>>this.DataSet = new dsCustomers();<</b>>
}

wwBusiness then uses this instance of the DataSet instead of the stock dataset to fill all data.

When you return data from any methods you can then cast the DataSet and DataRow to the appropriate type of the Typed DataSet. The most common case is to cast a DataRow:

Customer = new busCustomer();

Customer.Load(1000);  // Load by PK

// *** Cast the DataRow to a custom typed DataRow
<<b>>dsCustomers.wws_customersRow CustRow = (dsCustomers.wws_customersRow) Customer.DataRow;<</b>>

// *** Used the typed row 
Response.Write( CustRow.address );
CustRow.address = "New Address";

Customer.Save();

Of course you can also cast the DataSet itself and then drill into it from there through the typed DataTable:

// *** Cast to your typed DataSet
<<b>>dsCustomers Customers = (dsCustomers) Customer.DataSet;<</b>>

// *** Get a custom DataRow (standard typed DS behavior)
dsCustomer.wws_CustomerRow CustRow = dsCustomers.wws_customers[0];

As you can see this process is not automated through the business object - you manually must cast the items in order to utilize the typed DataSet. I suspect the first scenario is the most common one where you want to cast the DataRow to a specific type.

Important warning
Note it's important to understand that this only works if the DataRow and the custom typed datarow members point at the same data structure. For example, if you selected select * from a table, later add some fields you will have to make sure that the typed DataRow gets regenerated to reflect those new fields. Typed DataSets depend on exact matches of the underlying data in the DataRow at least alternately a cast from DataRow to a typed DataRow will result in null. This is the main reason why the Web Store does not use typed datasets as it is very easy to make datachanges and forget to update the schema.


© West Wind Technologies, 1996-2018 • Updated: 12/07/03
Comment or report problem with topic