1. Home
  2. Computing & Technology
  3. Visual Basic
Using ADO .NET - More DB Updating - Part 5
5 - Autonumber Complications
 More of this Feature
• 1 - Changing Without Wizards
• 2 - ADO .NET: Built for Networks
• 3 - A Few Words About Design
• 4 - The CommandBuilder
• 6 - The Code and Download
 
 Join the Discussion
Is this the kind of article that helps you?
Let us know!
 
 Related Resources
• Part 1 of the ADO .NET Series
• Part 2 of the ADO .NET Series
• Part 3 of the ADO .NET Series
• Part 4 of the ADO .NET Series
• VB .NET Books!
• New to VB .NET?
  Learn It Here!
 
 Elsewhere on the Web
• About.Com DATABASES
• MSDN Technical Article: CommandBuilder object
 

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

Explore Visual Basic
By Category
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

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

All rights reserved.