Strongly Typed DataSetsUntil now, we have been discussing DataSets in their untyped form. However, with ADO.NET and Visual Studio, we can also generate typed DataSets. A typed DataSet is derived from the untyped DataSet class but adds objects, methods, properties, and events that are specific to our database schema. The schema is defined in an XML Schema file (.xsd file), and a design-time tool is provided to generate a set of classes based on this schema. Because the typed DataSet is derived from the DataSet, it inherits all its functionality, properties, methods, and events, and it can be used anywhere an untyped DataSet can be used. However, as its elements are strongly typed, it offers additional features that provide increased development speed and reliability.
For example, using our Customers table, setting a column value for a row would look like dsCustomers.Tables("Customers").Rows(row)("FirstName") = NewValue where row is the index into the Rows collection of the Customers table, FirstName is the name of the column being accessed, and NewValue is the new value being assigned. Several points of potential errors will only be reported at run-time rather than at design time. Is the variable NewValue of the correct type to match the type of the column being assigned? Does the table Customers exist? Does the column FirstName exist? Was the table or column name accidentally misspelled? The same questions apply to reading a column value. However, if we generate a typed dsCustomers DataSet, the DataSet has properties specific to our schema and can already do all the required type checking at design time. The corresponding code for a typed DataSet containing the Customers table would be dsCustomers.Customers(row).FirstName = NewValue Note how the Customers table is a specific property of the typed DataSet and the FirstName column is a specific property of the typed DataTable Customers. We also show shortly how the IntelliSense in the code editor utilizes these properties. Let's return to the Departments table to see how this approach works. First, we do the following.
We now need to add the desired schema to our project. If we already had an .xsd file, we could use that. As we don't have one, we'll create one. We can do so easily by using the components from the Data tab of the toolbox, as follows.
The Dataset Generator also added the XML schema file, DepartmentsDS.xsd, to the project in the Solution Explorer. In addition, the file that will implement the custom typed DataSet classes, DepartmentsDS.vb, was added to the project beneath the schema file. The DepartmentsDS.vb file isn't visible unless we click on the Show All Files button on the top of the Solution Explorer. Note The DataSet Generator nd Schema Editor handle schemas with related tables as easily as they handle single tables.
Note If you're curious, go ahead and open the DepartmentsDS.vb file in the code editor to see what the generated code looks like. The important thing to remember is that for a given table name (which we changed from tblDepartment to Departments) the DataSet Generator generates three object classes, DepartmentsDataTable DepartmentsDataRow DepartmentsRowChangeEvent in addition to the type DataSet class itself, which we specified as DepartmentsDS. We can now go ahead and add the code to use the typed DepartmentsDS DataSet, as shown in Listing 7.8. Listing 7.8 Code to display the contents of the typed DataSet DepartmentsDSPrivate Sub btnTypedDataSet_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnTypedDataSet.Click daDepartments.Fill(dsDepartments, "Departments") DisplayDepartments(dsDepartments) End Sub Private Sub DisplayDepartments(ByVal ds As DepartmentsDS) Me.lstOutput.Items.Clear() Me.lstOutput.Items.Add("DISPLAY TYPED DATASET") Me.lstOutput.Items.Add("=====================") 'Each Column is now a property of the DepartmentsDS DataSet Dim row As DepartmentsDS.DepartmentsRow For Each row In ds.Departments.Rows Me.lstOutput.Items.Add( _ ds.Departments.IDColumn.ColumnName _ & " : " & row.ID.ToString) Me.lstOutput.Items.Add( _ ds.Departments.DepartmentNameColumn.ColumnName _ & " : " & row.DepartmentName) Next End Sub In the Click handler for btnTypedDataSet, we call the Fill method of the daDepartments DataAdapter to load the Department table with data. We then call the DisplayDepartments routine to display the contents of the table. To display the table contents, we simply loop across ll the rows in the table and display the column name and value for each column. This approach isn't very different from what we've done before, but note the following.
Figure 7.8 shows how typed properties (for example, DepartmentName) appear in the IntelliSense pop-up menu, which aids and accelerates the coding process. Figure 7.8. IntelliSense menu, showing properties of a typed DataSet |