Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Creating a Web Application Using a Typed DataSet

First, delete the default WebForm shown as WebForm1.aspx in the Solution Explorer. Create a new WebForm by right-clicking the solution and then choosing Add, Add Web Form. Name the new WebForm TDSTest.aspx. Right-click the TDSTest.aspx in Solution Explorer, and click Set As Start Page so that when we build our application, this starts with our testing page.

Now drag and drop the DataGrid from toolbox onto the surface of TDSTest.aspx.

Right-click the DataGrid and select Auto Format to choose the predefined color and format schemes for the DataGrid (see Figure 5.) Note that you can use the default format of the DataGrid or change it. For our example, we chose the scheme named Colorful 5. Now press the F7 key to switch to the code-behind file.

Figure 5Figure 5 Creating a DataGrid and setting its format.

Place the code shown below in the Page_Load method:

String connectionString =

SqlConnection conn = new SqlConnection(connectionString);

SqlDataAdapter EmployeesDA =
new SqlDataAdapter("SELECT * FROM EMPLOYEES", conn);

EmployeesTDS tdsEmployees = new EmployeesTDS();
EmployeesDA.Fill(tdsEmployees, "Employees");

// Here you can see that we are choosing the
//Employees table by name, unlike
// tdsEmployees.Tables[0] or tdsEmployees.Tables["Employees"]
//for a simple DataSet
DataGrid1.DataSource = tdsEmployees.Employees;

Now we can try the first phase of our application by clicking the Start button under Debug in the main menu (or by pressing the F5 key). After successful compilation, you can also access this with http://localhost/TypedDS/TDSTest.aspx. Happy output!

It doesn't seem to be a great benefit to be able to just choose the table name with the name, but trust me, it is. When you are programming a highly data-driven application, you will see the value of this benefit; even now you can use the Intellisense feature to pick out the table name from the collection of tables in the DataSet. At least in this case you do not need to remember the name of the table or, in case of multiple tables, the index of the table inside the DataSet. That would look something like this in the case of a normal DataSet:

// dsEmployees is a simple DataSet containing the Employees table
DataGrid1.DataSource = dsEmployees.Tables["Employees"];


DataGrid1.DataSource = dsEmployees.Tables[0];

As you can see, to use the first case, you must remember the correct name of the table; any spelling mistake is not reported as an error at the time of compilation. In the second case, you have to remember the index of the table inside the DataSet. (Well, currently we have only one table, so this is not really difficult to remember an index of 0. For multiple tables, though, this can be an issue.)

Now we come to the next phase of our application. We assume that readers are familiar with the concept of DataGrid events, and we will not discuss how to use them here.

In case, we want to use our Typed DataSet inside an event handler. For example, we use the OnItemDataBound DataGrid event, and we want to make some operations on the current DataGridItem.

With a normal DataSet, we do something like this to get the current DataGrid item as DataRow (DataRowView) to perform required operations:

DataRowView CurrentRow = (DataRowView) e.Item.DataItem;

Column values are used like this:

String FirstName = (String) ObjRowVals["FirstName"];

Then how do we use our Typed DataSet to do the same in a more safe way? Here is the solution:

DataRowView ObjRowView = (DataRowView) e.Item.DataItem;
EmployeesTDS.EmployeesRow CurrentRow = (EmployeesTDS.EmployeesRow) ObjRowView.Row;

Now we can access any column with its name—for example, FirstName:

CurrentRow.FirstName or CurrentRow.LastName (for LastName)

So this is all the underlying benefit. This shows that we do not have only an EmployeesTDS class; instead, we have much control by using an EmployeesRow class and so on. You can take a look at the available classes and interfaces in "Class View" by choosing it from "View" in the main menu of Visual Studio.NET. (See Figure 6.)

Figure 6Figure 6 ClassView of EmployeesTDS.

As a final note, when we are using Typed DataSet, we do not need to cast values back to their original data type. For example, in the case of a normal DataSet, we have to do so something like this:

String FirstName = (String) ObjRowView["FirstName"];

While using Typed DataSet:

String FirstName = CurrentRow.FirstName;
  • + Share This
  • 🔖 Save To Your Account