Adding a DataSet Object


Adding a DataSet Object

Actually, there are two ways to add the DataSet object to our project. We could grab it from the Data tab shown on the Toolbox, but that's the hard way. Instead, select the Data, Generate Dataset menu sequence shown in Figure 25.17.

Figure 25.17. The Generate Dataset menu sequence.

graphics/25fig17.jpg

After making the menu selection, the screen shows you the options in Figure 25.18.

Figure 25.18. Dialog box for adding the DataSet object to the project.

graphics/25fig18.jpg

Visual Basic .NET is smart enough to know that we haven't added a DataSet object to our project yet. If we had, the Existing drop-down list box would present those DataSet objects that have been added to the project. Because that isn't the case, it adds a DataSet object named DataSet1 for us.

The large text box near the middle of the figure presents a list of the tables we want associated with this DataSet object. As you can see, Visual Basic .NET got the information from the OleDbDataAdapter we built earlier. Because we elected to use only the Customer table when we configured the data adapter, that table is automatically checked for us.

The innocent check box that says Add This Dataset to the Designer determines whether we want the requisite code to use the DataSet object added to the project. Life is a lot simpler if you leave it checked. Otherwise, you must add the DataSet object code yourself. You can do this, but why bother if Visual Basic .NET will do it for you? Now click OK.

Visual Basic .NET will grind away for a few seconds while it adds the new code to your project. Eventually, your display will look like that shown in Figure 25.19.

Figure 25.19. The project after the DataSet object has been added.

graphics/25fig19.jpg

Notice that all three required components we talked about in Chapter 24 (see Figure 24.3) are now present in our project. We have to add only a few more statements to complete the project.

Adding the DataGrid Control

Obviously, we want to use a DataGrid control to display the output from our database query. Click on the Toolbox-Windows Forms tab and double-click the DataGrid control to place it on our form.

The first thing you need to do is bind the DataGrid control to our DataSet1 object. You do this by setting the DataSource property of the DataGrid control. When you move to the DataSource property in the Properties window, you'll see the DataSet object that's associated with the Customer table. Click on DataSet1.Customer option. Your screen should now look similar to Figure 25.20.

Figure 25.20. The DataGrid control after setting the DataSource property.

graphics/25fig20.jpg

Notice how the column headers are filled in for us by Visual Basic .NET. You'll also see that the last column is Customer ID. We didn't ask for this field, so why is it there? The reason is because Customer ID is the primary key field for the Customer database table. (Refer to Figure 24.1 to see the primary fields for the Xtreme.mdb database.) Visual Basic .NET automatically adds the primary key field for us.

As a starting point, we set the DataGrid 's size to 500 pixels wide by 200 high. We also set the AlternatingBackColor-Web to GreenYellow from the predefined list of colors. Suit yourself.

Next, we changed the Text property of the form to "DBWizard Test" . Finally, we need to link the DataAdapter object to the DataSet object. You do this by adding one statement to the Form1 Load event. The entire program code is presented in Listing 25.1.

Listing 25.1 The DBWizard Project Source Code
 Public Class Form1  Inherits System.Windows.Forms.Form  ' Windows Form Designer generated code  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _              System.EventArgs) Handles MyBase.Load   OleDbDataAdapter1.Fill(DataSet11)  End Sub End Class 

Now compile and run the project. You should see output similar to that shown in Figure 25.21. (We expanded the columns a bit.)

Figure 25.21. Sample run of the DBWizard project.

graphics/25fig21.jpg

Congratulations! You've just written your own program to read an Access database. Well actually you wrote one line of code and Visual Basic .NET wrote the rest of it.

As I said in the first paragraph of this chapter, there are two ways to build a program that can read a database, and we just finished the first way. Using the Visual Basic .NET wizards is the "You-don't-need-to-know-how-to-build-a-car-to-drive-one" approach to database program development. This approach works fine until your car breaks down in the middle of nowhere. It's my belief that you need to know a little more about what's going on under the hood to appreciate how to really use Visual Basic .NET with databases. That's the topic of the remainder of this chapter. We'll also revisit the wizard again in this chapter.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net