An Introduction to VB.NET and Database Programming

The Recipe Application

The Murach book showed me how to create a Connection, Data Adapter, and a Dataset in Chapter 17. In Chapter 18 it discussed how to bind controls to the data and then use bound controls to update, add, and delete data rows. Parameterized queries tied everything together for the Recipe Application. As you can see below I have created two DataAdapters and a Dataset for each. I need to keep the Category table and the Recipe Table separate. There may be a better way to do this, but this worked.

One of the most important things I learned from the book is that you must fill the dataset. With VB.NET you are not working directly with the data like you would using MS Access. You are working with a 'picture' of the data created by your SQL statements in the Data Adapter and then filled into the Dataset. You can load the data in any event of the form - a button click, a combo box selection or a form load event. In most cases you will load the data in the form load as seen below.

Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

DaCategory.Fill(DsCategories1)
DaRecipes.Fill(DsRecipes1)
Me .SetEntryControls( False )
Me .SetMaintenanceButtons( True )
RecipesBindingManager = Me .BindingContext(DsRecipes1, "Recipes" )
     btnEdit.Enabled = False
     lblCategoryText.Visible = False
     txtCategory.Visible = False

End Sub

The important lines in filling the data are the two listed in red. The other line that was critical to using the combo box to navigate records was the line in green. The binding manager tracks the position or row of data that the application is currently viewing or potentially editing. You should also notice the Me.SetEntryControls(False) line of code. This is calling another sub routine that sets the enabled or disabled status of certain controls on the form. Each control could be listed individually, but if you have several controls that are always going to be enabled or disabled as a group then making a separate sub for that update will make it easier for you to enable and disable them throughout your application.

Private Sub SetComboBoxControls( ByVal bComboMode As Boolean )
     cboCategory.Enabled = bComboMode
     cboRecipe.Enabled = bComboMode
End Sub

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Memory is like an orgasm. It's a lot better if you don't have to fake it.” - Seymour Cray