Typed DataSets ADO.NET: Developing Database Applications

368 html body asp:DataGrid id=grdCustomers runat=server ForeColor=Black AlternatingItemStyle BackColor=Gainsboro FooterStyle ForeColor=White BackColor=Silver ItemStyle BackColor=White HeaderStyle Font-Bold=True ForeColor=White BackColor=Navy asp:DataGrid body html Figure 8-6. The display generated by the code in Ex a m ple 8 - 9 Note the following: • Unlike the Windows Forms DataGrid class, the Web Forms DataGrid class has no SetDataBinding method. Instead, set the Web Forms DataGrids DataSource property and then call the DataGrids DataBind method. • Unlike the Windows Forms DataGrid class, the Web Forms DataGrid classs DataSource property cant directly consume a DataTable or DataSet. Instead, the data must be wrapped in a DataView or DataSetView object. The properties and methods of the DataView and DataSetView classes provide additional control over how data is viewed in a bound DataGrid. DataView and DataSetView objects can be used by either Windows Forms or Web Forms DataGrids, but they are mandatory with Web Forms DataGrids. • The DataGrids DataSource property can consume any object that exposes the System.Collections.ICollection interface.

8.10 Typed DataSets

There is nothing syntactically wrong with this line of code: Dim dt As System.Data.DataTable = ds.TablesCustumers However, Custumers is misspelled. If it were the name of a variable, property, or method, it would cause a compile-time error assuming the declaration were not similarly misspelled. However, because the compiler has no way of knowing that the DataSet ds will not hold a table called Custumers , this typographical error will go unnoticed until runtime. If this code path is not common, the error may go unnoticed for a long time, perhaps until after the software is delivered and running on thousands of client machines. It would be better to catch such errors at compile time. Microsoft has provided a tool for creating customized DataSet-derived classes. Such classes expose additional properties based on the specific schema of the data that an object of this class is expected 369 to hold. Data access is done through these additional properties rather than through the generic Item properties. Because the additional properties are declared and typed, the Visual Basic .NET compiler can perform compile-time checking to ensure that they are used correctly. Because the class is derived from the DataSet class, an object of this class can do everything that a regular DataSet object can do, and it can be used in any context in which a DataSet object is expected. Consider again Ex am ple 8- 1 , shown earlier in this chapter. This fragment of code displays the names of the customers in the Northwind database that are located in London. Compare this to Ex am ple 8- 10 , which does the same thing but uses a DataSet-derived class that is specifically designed for this purpose. Example 8-10. Using a typed DataSet Open a database connection. Dim strConnection As String = _ Data Source=localhost;Initial Catalog=Northwind; _ Integrated Security=True Dim cn As SqlConnection = New SqlConnectionstrConnection cn.Open Set up a data adapter object. Dim strSelect As String = SELECT FROM Customers WHERE City = London Dim da As New SqlDataAdapterstrSelect, cn Load a data set. Dim ds As New LondonCustomersDataSet da.Fillds, LondonCustomers Close the database connection. cn.Close Do something with the data set. Dim i As Integer For i = 0 To ds.LondonCustomers.Count - 1 Console.WriteLineds.LondonCustomersi.CompanyName Next Note that in Ex am ple 8- 10 , ds is declared as type LondonCustomersDataSet, and this class has properties that relate specifically to the structure of the data that is to be loaded into the DataSet. However, before the code in Ex am ple 8- 10 can be written, it is necessary to generate the LondonCustomersDataSet and related classes. First, create an XML schema file that defines the desired schema of the DataSet. The easiest way to do this is to write code that loads a generic DataSet object with data having the right schema and then writes that schema using the DataSet classs WriteXmlSchema method. Ex am ple 8- 11 shows how this was done with the LondonCustomers DataSet. Example 8-11. Using the WriteXmlSchema method to generate an XML schema This code is needed only once. Its purpose is to create an .xsd file that will be fed to the xsd.exe tool. Open a database connection. Dim strConnection As String = _ Data Source=localhost;Initial Catalog=Northwind; _ Integrated Security=True Dim cn As SqlConnection = New SqlConnectionstrConnection cn.Open 370 Set up a data adapter object. Dim strSelect As String = SELECT FROM Customers WHERE City = London Dim da As New SqlDataAdapterstrSelect, cn Load a data set. Dim ds As New DataSetLondonCustomersDataSet da.Fillds, LondonCustomers Close the database connection. cn.Close Save as XSD. ds.WriteXmlSchemac:\LondonCustomersDataSet.xsd Next, run Microsofts XML Schema Definition Tool xsd.exe against the XML schema file you just generated. Here is the command line used for the LondonCustomers DataSet: xsd d l:VB LondonCustomersDataSet.xsd The d option indicates that a custom DataSet and related classes should be created. The l:VB option specifies that the generated source code should be written in Visual Basic .NET the tool is also able to generate C source code. With this command line, the tool generates a file named LondonCustomersDataSet.vb, which contains the source code. Finally, add the generated .vb file to a project and make use of its classes.

8.11 Reading Data Using a DataReader