Viewing the Contents of a Dataset

Open Forml.vb in design mode, and from the Windows Forms section of the Toolbox, drag a DataGrid to the form. Just for presentation, add a label to the top of the form saying The CustomerOrders Dataset.

Next add an OleDbDataAdapter to the form by dragging and dropping one from the Data section of the Toolbox. This action opens the Data Adapter Configuration Wizard, shown in Figure 7-26, which will help you step through the process of setting up your data adapter. The first thing you will have to do is select your data connection. Make sure this is the same as the datasource you built your dataset from, in this case, the Northwind database.

If it is not the default, enter the details for your Northwind database. Click New Connection to open the Data Link Properties window. Enter the location of your SQL Server and the security settings, and select Northwind from the list of available databases. Test the connection, and if all is good, click OK to continue setting up the OleDbDataAdapter.

Figure 7-26

The next dialog allows you to choose a query type to decide how the data adapter will query the database. We will use SQL statements for this purpose, so select Use SQL statements. Click Next to get to the Generate the SQL Statements dialog, and then click Query Builder to generate your statement. This opens the Query Builder, and in front of it, the Add Table dialog, as shown in Figure 7-27.

TO Table E)

Table; views Functions

Categories-

Customers

Employees

Invoices

Order Details

Orders

Products

Shippers

Suppliers

Add | Close Help

Add the Customers and Orders tables that make up our dataset and click Close, and the SQL query will start to be built. Then simply check the (All Columns) boxes in both of the tables to complete our query (Figure 7-28).

Figure 7-28

Click OK and then Finish, to complete configuration of the data adapter.

Now that the datasource has been defined, add a data grid (if you haven't already), double-click the form (not in DataGridl) to open up the code designer for Forml.vb. This action creates a procedure called Form1_load, which fires when the form is loaded. Insert the following code into this procedure:

Private Sub Form1_Load(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles MyBase.Load Dim CustomerOrders As New DataSet() OleDbDataAdapterl.Fill(CustomerOrders) DataGrid1.SetDataBinding(CustomerOrders, "Customers")

End Sub

This code will use the data adapter we have just set up to fill the dataset with information from the Customers table in the Northwind database, and display it in DataGridl.

Working with ADO .NET requires two namespaces: System.Data, and because we are using OLEDB, the System.Data.OleDb namespace as well. Add the Imports statements to the top of Forml.vb.

Imports System.Data Imports System.Data.OleDb

Public Class Form1

Inherits System.Windows.Forms.Form

Run the code and your application should open and display the dataset it has been loaded with, providing you with access to data from the CustomerOrders dataset, as shown in Figure 7-29.

Forml EiDE)"

The CustomerOrders Daitfiset

Was this article helpful?

0 0

Post a comment