An Introduction to VB.NET and Database Programming

Data Binding

Actually binding data to a control is very easy in the IDE. Just select the control on the form and then go to the properties pane and select the DataSource and the display member. These will both be drop downs and the datasets you have created will be selectable. The DataSource is the name of your Dataset.Table (so in the case of my category combo box the DataSource was DsCategories.Categories. The Display member is the actual field in the table that you want to be bound to that field and displayed. In the Recipe application that was category. In order to get this the selected category has to be passed to the DataAdapter that populates the Recipe DataSet. To do that I used the code below:

Private Sub cboCategory_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCategory.SelectedIndexChanged

If bNewRow Then
         txtCategory.Text = cboCategory.Text
Else
         DsRecipes1.Clear()
DaRecipes.SelectCommand.Parameters( "Category" ).Value = cboCategory.Text
         DaRecipes.Fill(DsRecipes1)
         cboRecipe.Focus()
End If

End Sub

The code above clears and refills DsRecipes1. The line in red sends a value to the DataAdapter which is parameterized. The remainder of the application uses the binding manager and bound controls to navigate through the data, edit the data or add new data.

The important thing to remember when you are trying to add, delete, or edit data is that your application is working with its own DataSet not the actual database. To update the data you must use a command to take the current dataset and update the database with it. Here is the code that I used to accomplish this:

Private SubUpdateDatabase()
     DaRecipes.Update(DsRecipes1.Recipes)
End Sub

Any code that updates the database uses the above code by containing Me.UpdateDatabase() . This code is included in the update and delete button code of the Recipe Application code. Below is the code for the delete button so you can see it in action:

Private Sub btnDelete_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

Dim iResult As DialogResult = MessageBox.Show( "Delete " & txtTitle.Text & " ?" , "Confirm Delete" , MessageBoxButtons.YesNo, MessageBoxIcon.Question)

If iResult = DialogResult.Yes Then
         RecipesBindingManager.RemoveAt(RecipesBindingManager.Position)
Me.UpdateDatabase()
         cboCategory.SelectedIndex = -1
Me.SetMaintenanceButtons( True )
Me.SetEntryControls( False )
Me.SetComboBoxControls( True )
         cboCategory.Focus()
         btnDelete.Enabled = True
End If
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.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook