One of the problems (and complications) that you will have to work with is the fact that our Access database uses an AutoNumber field for the key. This means that if a new key, or no key at all, is sent to the database, our ever-helpful DataAdapter Update method will simply consider it to be a new record again and add a new row to the table instead! Consequently, when a selection is made in the ListBox, the key value (which is not displayed or is otherwise significant to the user) is simply saved in a variable and then assigned back to the same field. This is done by the SelectedIndexChanged event which will be covered in few paragraphs. Problems like this can vary by database and make ADO .NET not completely transparent to different databases - even though it comes remarkably close.
The final If clause is the simplest of all because it only has to delete whatever row has been selected in the ListBox from the database:
myOleDbDataAdapter.Fill(myDataSet, "Articles")
myDataTable = myDataSet.Tables("Articles")
myDataTable.Rows(lstAboutVBData.SelectedIndex).Delete()
myOleDbDataAdapter.Update(myDataSet, "Articles")
myDataSet.Tables("Articles").AcceptChanges()
RefreshListBox()
The last thing that needs to be mentioned is the code that makes sure that the TextBox and CheckBox components are refreshed when a ListBox selection is changed. It looks a lot like the other code we've seen and the only unique thing about it is that the key value is saved here for reuse when a record is changed.
Dim myDisplayRow As DataRow = myDataTable.Rows(lstAboutVBData.SelectedIndex)
artKeySave = myDisplayRow("artKey")
txtTitle.Text = myDisplayRow("artTitle")
txtDate.Text = myDisplayRow("artDate")
chkVB6.Checked = myDisplayRow("artVB6")
chkVBNET.Checked = myDisplayRow("artVBNET")
txtURL.Text = Mid(myDisplayRow("artURL"), 2, Len(myDisplayRow("artURL")) - 2)
txtDescription.Text = myDisplayRow("artDescription")
It's worth pointing out again that because a goal of this example is to maintain full synchronization between the ADO .NET objects in the program and the database, some of the reinitialization statements might not be necessary in a program without this requirement.
Action queries which add, change, and delete data are among the most difficult to get to work correctly and ADO .NET reaches a new high in making this technology as easy to use as possible. This is one approach to getting ADO .NET action queries to work but as you write your own programs, you'll see that it isn't the only one.
See the next page for downloads and the complete code.
Next page >
The Code and Download > Page
1,
2,
3,
4,
5,
6