1. Home
  2. Computing & Technology
  3. Visual Basic
Using ADO .NET - More DB Updating - Part 5
6 - The Code and Download
 More of this Feature
• 1 - Changing Without Wizards
• 2 - ADO .NET: Built for Networks
• 3 - A Few Words About Design
• 4 - The CommandBuilder
• 5 - Autonumber Complications
 
 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
 

Download the code.

The Completed Program
Imports System.Data.OleDb

Public Class frmAboutVBSpotlights
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    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

    Private Sub frmAboutVBSpotlights_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load
        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)
    End Sub

    Private Sub cmdUpdate_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles cmdUpdate.Click
        If Microsoft.VisualBasic.Left(cmdUpdate.Text, 5) = "First" _
        Then
            myOleDbDataAdapter.Fill(myDataSet, "Articles")
            myDataTable = myDataSet.Tables("Articles")
            Dim rowArticle As DataRow = myDataTable.NewRow()
            rowArticle("artTitle") = txtTitle.Text
            Try
                rowArticle("artDate") = txtDate.Text
            Catch
                rowArticle("artDate") = Now()
            End Try
            rowArticle("artVB6") = chkVB6.CheckState
            rowArticle("artVBNET") = chkVBNET.CheckState
            rowArticle("artURL") = txtURL.Text
            rowArticle("artDescription") = txtDescription.Text
            myDataTable.Rows.Add(rowArticle)
            myOleDbDataAdapter.Update(myDataSet, "Articles")
            myDataSet.Tables("Articles").AcceptChanges()
            cmdUpdate.Text = "Then, Select And Change An Article"
            RefreshListBox()
        ElseIf Microsoft.VisualBasic.Left(cmdUpdate.Text, 5) = "Then," _
        Then
            myOleDbDataAdapter.Fill(myDataSet, "Articles")
            myDataTable = myDataSet.Tables("Articles")
            Dim myChangeRow As DataRow = _
                myDataTable.Rows(lstAboutVBData.SelectedIndex)
            myChangeRow("artKey") = artKeySave
            myChangeRow("artTitle") = txtTitle.Text
            Try
                myChangeRow("artDate") = txtDate.Text
            Catch
                myChangeRow("artDate") = Now()
            End Try
            myChangeRow("artVB6") = chkVB6.CheckState
            myChangeRow("artVBNET") = chkVBNET.CheckState
            myChangeRow("artURL") = txtURL.Text
            myChangeRow("artDescription") = txtDescription.Text
            Dim newDataSet As DataSet
            newDataSet = myDataSet.GetChanges()
            myOleDbDataAdapter.Update(newDataSet, "Articles")
            myDataSet.Tables("Articles").AcceptChanges()
            cmdUpdate.Text = "Finally, Select And Delete An Article"
            RefreshListBox()
        ElseIf Microsoft.VisualBasic.Left(cmdUpdate.Text, 5) = "Final" _
        Then
            myOleDbDataAdapter.Fill(myDataSet, "Articles")
            myDataTable = myDataSet.Tables("Articles")
            myDataTable.Rows(lstAboutVBData.SelectedIndex).Delete()
            myOleDbDataAdapter.Update(myDataSet, "Articles")
            myDataSet.Tables("Articles").AcceptChanges()
            RefreshListBox()
        End If
    End Sub

    Private Sub lstAboutVBData_SelectedIndexChanged( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles lstAboutVBData.SelectedIndexChanged
        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")
    End Sub

    Private Sub RefreshListBox()
        myDataSet = New DataSet()
        myDataTable = New DataTable()
        myOleDbDataAdapter.Fill(myDataTable)
        lstAboutVBData.DataSource = myDataTable
        lstAboutVBData.DisplayMember = "artTitle"
        lstAboutVBData.ValueMember = "artKey"
    End Sub
End Class

First page > Changing Without Wizards > Page 1, 2, 3, 4, 5, 6

Explore Visual Basic
By Category
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.