A few words about design are important here.
Each time the data in the form is changed and the button is clicked, a "round trip" of changing the data in the form and then permanently posting the change to the database is done. This may not be the best design for your application since you may want to accumulate more data 'on the client side' before reconnecting with a database to actually make these changes 'on the server side'. ADO .NET is designed to help you do this. This program is an example to illustrate what a round trip looks like in each case.
The first part of the program should be familiar to you since all we do is create a DataSet and populate properties in form components using the data in the DataSet. But before anything else, is coded, an Imports statement is added at the top to simplify coding ...
Imports System.Data.OleDb
and several global declarations are added since these objects will be used throughout the program. And the global variables artKeySave that we'll need for updating the database is here rather than passing it as a subroutine parameter.
Dim myOleDbDataAdapter As OleDbDataAdapter
Dim myDataSet As DataSet = New DataSet()
Dim myDataTable As DataTable = New DataTable()
Dim myOleDbConnection As OleDbConnection
Dim myCommand As OleDbCommandBuilder
Dim artKeySave As Integer
In the form Load event, the form is initialized and the artTitle and artKey database fields are bound to the ListBox control. (Note ... You must make sure that {path} in the connection object is changed to match your own file configuration.)
myOleDbConnection = New _
OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "User ID=Admin;Data Source={path}\AboutVB.mdb")
Try
myOleDbDataAdapter = _
New OleDbDataAdapter("select * from Articles", _
myOleDbConnection)
myOleDbDataAdapter.Fill(myDataTable)
lstAboutVBData.DataSource = myDataTable
lstAboutVBData.DisplayMember = "artTitle"
lstAboutVBData.ValueMember = "artKey"
Catch
Console.WriteLine("Error Opening {0}", _
myOleDbConnection.DataSource)
End Try
myCommand = New OleDbCommandBuilder(myOleDbDataAdapter)
The ListBox properties are the same ones that were set using wizards and in the properties dialog, but this time they're being set at 'run time' under program control instead of at 'design time'. Notice also that we Fill a DataTable and use that as our DataSource. A DataTable is simply one part of a DataSet and also helps illustrate the flexibility of the new ADO .NET objects.
Next page >
The CommandBuilder > Page
1,
2,
3,
4,
5,
6