Using the Data Form Wizard
Visual Basic .NET includes a tool to help introduce you to ADO.NETthe Data Form Wizard. In this section, you're going to use the Data Form Wizard to create a form that is bound to the same database you used in the previous example.
Start by creating a new Windows Application titled Data Form Example. The Data Form Wizard is run by adding it to your project as a form template. Choose Add Windows Form from the Project menu to display the Add New Item dialog box, click the Data Form Wizard icon, change the name to fclsDataForm.cs (see Figure 21.3), and click Open to start the wizard.
Figure 21.3 The Data Form Wizard as a form template.
The first page of the wizard is simply an introduction. Click Next to get to the first "real" page. This next page is used to choose the dataset you want to bind to the form. ADO.NET datasets hold a collection of DataTables. Enter AllContacts into the text box (see Figure 21.4) and click Next to continue.
Figure 21.4 A DataTable is similar to an ADO recordset.
The next page of the wizard is used to specify a connection to a data source (see Figure 21.5). Note: Because you haven't previously defined a connection to the Contacts database, your drop-down list will be empty. Click the New Connection button to display the Data Link Properties dialog box. Notice that this dialog box opens with the Connection page visible. Click the Provider tab to see the list of installed providers on your computer (see Figure 21.6), choose Microsoft Jet 4.0 OLE DB Provider to select it, and then click the Connection tab once more.
Figure 21.5 Use this page to specify a data source.
Figure 21.6 You must specify the appropriate provider for the type of data source to which you are connecting.
Now that you've selected the provider, you need to locate and select the data source (your Jet database). Click the build button next to the database name text box, and then locate and select the contacts.mdb database. Next, click Test Connection to make sure the information you have supplied creates a valid connection to the database. If the test succeeded, click OK to close the Data Link Properties dialog box. The database should now appear in the Connection drop-down list. Click Next to continue.
The next step in completing the wizard is to choose the table or tables you want to use (see Figure 21.7). The tables you choose here will be used to supply the data that is bound to your form. Double-click the Contacts table to add it to the Selected Items list and click Next to continue.
Figure 21.7 Use this page to choose the data to bind to the form.
This page shown in Figure 21.8 is used to specify the columns that you want bound on the form. The two columns in your Contacts table are already selected by default, so click Next to continue.
Figure 21.8 You don't have to select all the fields in a table.
The last step of the wizard is specifying the style in which you want the data to appear (see Figure 21.9). Because the previous example had you add individual controls for each column, leave the All Records in a Grid radio button, selected (this will create a data grid). Click Finish to create your new data form, which will appear in the form designer (see Figure 21.10).
Figure 21.9 The Data Form Wizard gives you a number of choices for displaying your data.
Figure 21.10 This bound grid was created by the Data Form Wizard.
To test your form, you'll have to display it. Click Form1.cs to display the designer for the default form in your project and add a new button to the form. Set the button's properties as follows:
|Text||Show Data Form|
Next, double-click the button to access its Click event and add the following code:
fclsDataForm objDataForm = new fclsDataForm (); objDataForm.Show();
Press F5 to run the project, and then click the button and your bound form will appear. To load the grid with records, click the Load button (see Figure 21.11).
Figure 21.11 This grid is bound to the record source.
Stop the running project, click fclsDataForm.cs in the Solution Explorer, and then click the View Code button on the Solution Explorer to view the class. Notice that the Data Form Wizard created all the ADO.NET code for you, and even included rudimentary error handling.
The Data Form Wizard is a great way to get started with ADO.NET, but it will take you only so far. To create robust ADO.NET applications, you will need to find one or more dedicated resources that focus on the intricacies of ADO.NET.