Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
Using ADO .NET - Using Wizards - Part 3
4 - Taking the Final Step
 More of this Feature
• 1 - Put Wizards On Your Side
• 2 - Stepping Up to the Wizard!
• 3 - Everything Old is New Again!
• 5 - Are We There Yet?
• 6 - The Code and Download Page
 
 Join the Discussion
Is this the kind of article that helps you?
Let us know!
 
 Related Resources
• Part 1 of the Tutorial
• Part 2 of the Tutorial
• VB .NET Books!
• New to VB .NET?
  Learn It Here!
 
 Elsewhere on the Web
• About.Com DATABASES
 

Step 8 -

The final step (before we start setting coding the old way again) is to generate the Dataset object that will actually provide the data to the components in the program. To do this, we will "right-click" on the data adapter component that the wizard added to the form and do this "automagically" again. But before we do that, it's a good idea to browse the other things that you can do when you right-click both the data adapter and connection objects.

The most interesting context menu options are the ones with ellipsis (the three dots at the end) indicating that another menu will appear if the option is clicked. For the data adapter object, these are:

  • Configure Data Adapter ...
  • Generate Dataset ...
  • Preview Data ...

The first one simply takes you through the same wizard we have already completed, but in a mode that allows changes to be made. If you click through this (Try it. No harm is done if you don't make any changes.) you will discover that

Select * From Articles

that we entered as a SQL statement has been changed to

SELECT artDate, artDescription, artKey, artTitle, artURL, artVB6, artVBNET FROM Articles

This is just another example of how Visual Studio is always watching and doesn't necessarily do exactly what you code, but rather what it thinks you should have coded. For those who learned to program with computers that always did EXACTLY what you coded, regardless of how stupid it might have been, this can require a shift of thinking.

The Preview Data ... option can be very useful to examine the data that is provided by the Dataset object. And check out the Properties of both the data adapter object and the connection object.

The option we're going to use, however, is the Generate Dataset ... option. There's not too much to do here. Simply select the option, click OK, and an object named Dataset11 (unless you change it) will also be added to the form.

Now it's time to "bind" these objects to some components.

You might want to take the time to add the rest of the components to the form that we'll need to duplicate the example from Part 2 of this series just for practice. If you don't think you need the practice, then the easy way to do it is to simply open another Visual Studio session, open the project from Part 2, drag over the entire set of components in the form (you can select them all at once), and drop them onto the form in the project we're building now. You'll discover that the components themselves are copied, but none of the code in the Part 2 project. Just what we need!

When you have added the components to the form, select the textbox (lstAboutVBData) and open the properties dialog. The three properties we'll have to change are DataSource, DisplayMember, and ValueMember. Considering DataSource first, notice that you have two choices:

Articles.DataSet11

and

DataSet11

This simply decides "where to start" in the database. If you select DataSet11, for example, then Articles is a choice in DisplayMember and ValueMember. This example is simple enough so that it doesn't matter which way we do it. Let's select DataSet11. Then select artTitle as the DisplayMember and artKey as the ValueMember.

The DisplayMember is the field in the datasource that will be shown in the list. The ValueMember is the field in the datasource that will be retrieved when you reference the SelectedValue for the control. When you bind data to a control, you must specify the contents of the ValueMember property.

These two properties can be confusing. Usually the ValueMember property is set to a field in the DataTable that is a key field. This can be useful because when the user selects a row in the listbox, it's easy to get that key field using the SelectedValue property of the listbox: ListBox1.SelectedValue.

Now, we're finally ready to enter the single line of actual code required by our program. Open the code window for the Load event of the form (double clicking on a blank part of the form will do it) and enter this line of code:

Me.OleDbDataAdapter1.Fill(Me.DataSet11)

Next page > Are We There Yet? > Page 1, 2, 3, 4, 5, 6

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.