The code below is modified from an example in a book using bound controls in an untyped dataset. two things happen that I don't understand. The first is that the table in the db doesn't get updated. No error thrown, but no update.
The second is is that when I click the Add button, the textboxes clear ok for a new entry. I enter a value in txtQuNum and tab to txtQuestion. But when I do that, the previous value that was just cleared reappears.
The textboxes appear to be bound ok. What's up? Thanks.
VB Code:
  1. Private Sub CreateADOObjects()
  2.         cn.ConnectionString = mstrFullPath
  3.         Try
  4.             Dim cmdQuestion As New OleDbCommand()
  5.             cmdQuestion.Connection = cn
  6.             Dim strQuestionSelect As String
  7.             strQuestionSelect = "SELECT * FROM QS"
  8.             cmdQuestion.CommandText = strQuestionSelect
  9.             daQuestion.SelectCommand = cmdQuestion
  10.  
  11.             Dim cmdModule As New OleDbCommand()
  12.             cmdModule.Connection = cn
  13.             Dim strModuleSelect As String
  14.             strModuleSelect = "SELECT * FROM [Mod List]"
  15.             cmdModule.CommandText = strModuleSelect
  16.             daModule.SelectCommand = cmdModule
  17.  
  18.             Dim cbQuestion As New OleDbCommandBuilder()
  19.             cbQuestion.DataAdapter = daQuestion
  20.             Dim cbModule As New OleDbCommandBuilder()
  21.             cbModule.DataAdapter = daModule
  22.         Catch ex As Exception
  23.             MessageBox.Show(ex.Message, "Creating Objects")
  24.         End Try
  25.     End Sub
  26.  
  27.     Private Sub FillTables()
  28.         Try
  29.             cn.Open()
  30.             dsQuestion.Clear()
  31.             daQuestion.Fill(dsQuestion, "QS")
  32.             dsModule.Clear()
  33.             daModule.Fill(dsModule, "[Mod List]")
  34.         Catch ex As Exception
  35.             MessageBox.Show(ex.Message & Environment.NewLine _
  36.                & "Ending Progranm", "Error Retrieving Data", _
  37.               MessageBoxButtons.OK, MessageBoxIcon.Error)
  38.             Application.Exit()
  39.         Finally
  40.             If cn.State = ConnectionState.Open Then
  41.                 cn.Close()
  42.             End If
  43.         End Try
  44.     End Sub
  45.  
  46.     Private Sub BindControls()
  47.         Try
  48.             txtQuestion.DataBindings.Add("Text", dsQuestion.Tables _
  49.               ("QS"), "QText")
  50.             txtQuNum.DataBindings.Add("Text", dsQuestion.Tables _
  51.               ("QS"), "Q#")
  52.         Catch ex As Exception
  53.             MessageBox.Show(ex.Message, "Binding Controls")
  54.         End Try
  55.     End Sub
  56.  
  57.     Private Sub GetData()
  58.         Try
  59.             CreateADOObjects()
  60.             FillTables()
  61.             bmbQuestion = Me.BindingContext(dsQuestion, "QS")
  62.             bmbModule = Me.BindingContext(dsModule, "[Mod List]")
  63.             BindControls()
  64.         Catch ex As Exception
  65.             MessageBox.Show(ex.Message, "Error Getting Data")
  66.         End Try
  67.     End Sub
  68.  
  69.     Private Sub btnAdd_Click(ByVal sender As System.Object,  _
  70.         ByVal e As System.EventArgs) Handles btnAdd.Click
  71.         Try
  72.             bmbQuestion.AddNew()
  73.             txtQuNum.Clear()
  74.             txtQuestion.Clear()
  75.             txtQuNum.Focus()
  76.         Catch ex As Exception
  77.             MessageBox.Show(ex.Message, "Error Adding Record")
  78.         End Try
  79.     End Sub
  80.  
  81.     Private Sub UpdateDB()
  82.         Try
  83.             daQuestion.Update(dsQuestion.Tables("QS"))
  84.             bmbQuestion.EndCurrentEdit()
  85.             dsQuestion.Clear()
  86.             daQuestion.Fill(dsQuestion, "QS")
  87.         Catch ex As Exception
  88.             MessageBox.Show(ex.Message, "Error Updating Database")
  89.         End Try
  90.     End Sub
  91.  
  92.     Private Sub btnSave_Click(ByVal sender As System.Object, _
  93.         ByVal e As System.EventArgs) Handles btnSave.Click
  94.         UpdateDB()
  95.     End Sub