Add a Typed dataset in Visual Studio as we did above from
add new item dialog box. Visual studio 2005 comes with a set of items in the Tool
box for constructing the typed dataset. The next sections will discuss how to
use the typed dataset as an alternative to C# custom business objects. Below
shows those items in Visual Studio 2005 Toolbox.
Figure 12– Visual studio Tool bar
1) Drag a Datatable object from the toolbox.
2) To add a column, right click the table object and click
Add>Column. Rename the column as you want.
Figure 13– Add a column
3) To specify datatype, click the properties of the
corresponding column and specify it.
So the final typed dataset will look like figure 14.
Figure 14
Using the constructed Typed Dataset:
Filling this typed dataset can be done in the same way as we
did above, keeping in mind that the database column name and datatype should
match with typed dataset. I will explain how to construct the typed dataset
programmatically instead of filling it from the database.
To create a new Products row us the following code.
Listing 7: Create new Prodcuts row
BO.Products.ProductsDataTable prod = new BO.Products.ProductsDataTable();
BO.Products.ProductsRow ProRow = prod.NewProductsRow();
Once we create a typed dataset and compile it, Framework
will create all the methods and properties that will help us in using the typed
dataset. The above lines of code “NewProductsRow()” function is created by the
framework which is the equivalent of “NewRow()” function of the normal
datatable.
The following table specifies the usual dataset elements and
typed dataset equivalent.
DataSet Element
|
Equivalent Typed DataSet Element
|
DataTable
|
DatabaseTableNameDataTable
|
DataTable methods
|
NewDatabaseTableNameRow
AddDatabaseTableNameRow
DeleteDatabaseTableNameRow
|
DataRowCollection
|
DatabaseTableName
|
DataRow
|
DatabaseTableNameRow
|
DataSet Events
|
DatabaseTableNameRowChangeEvent
DatabaseTableNameRowChangeEventHandler
|
The following code will set the value for the column.
Listing 8
ProRow.ProductName = txtProdName.Text;