I have a windows form that uses a listbox to navigate through a table in a database. The interface is meant to view, edit record, delete record, add record, and update the table in the database.

Whenever I input data and click add, the data overwrites whatever entry is presently selected in the listbox. This is despite changing the data for the primary key.

The other buttons work fine.

This is the code for the add button:

VB Code:
  1. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  2.         Try
  3.             'Clear out the current edits
  4.             Me.BindingContext(objDSPatients, "Biodata").EndCurrentEdit()
  5.             Me.BindingContext(objDSPatients, "Biodata").AddNew()
  6.         Catch eEndEdit As System.Exception
  7.             System.Windows.Forms.MessageBox.Show(eEndEdit.Message)
  8.         End Try
  9.         Me.objDSPatients_PositionChanged()
  10.     End Sub

This is the code for the listbox:
VB Code:
  1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
  2.         Try
  3.             Me.BindingContext(objDSPatients, "Biodata").Position = ListBox1.SelectedIndex
  4.         Catch dataException As Exception
  5.             MsgBox(dataException.Message)
  6.             Me.BindingContext(objDSPatients, "Biodata").CancelCurrentEdit()
  7.             Me.BindingContext(objDSPatients, "Biodata").Position = ListBox1.SelectedIndex
  8.         End Try
  9.     End Sub

This is the code relating the the dataset and all that:
VB Code:
  1. Public Sub UpdateDataSet()
  2.         'Create a new dataset to hold the changes that have been made to the main dataset.
  3.         Dim objDataSetChanges As Health1.DSPatients = New Health1.DSPatients
  4.         'Stop any current edits.
  5.         Me.BindingContext(objDSPatients, "Biodata").EndCurrentEdit()
  6.         'Get the changes that have been made to the main dataset.
  7.         objDataSetChanges = CType(objDSPatients.GetChanges, Health1.DSPatients)
  8.         'Check to see if any changes have been made.
  9.         If (Not (objDataSetChanges) Is Nothing) Then
  10.             Try
  11.                 'There are changes that need to be made, so attempt to update the datasource by
  12.                 'calling the update method and passing the dataset and any parameters.
  13.                 Me.UpdateDataSource(objDataSetChanges)
  14.                 objDSPatients.Merge(objDataSetChanges)
  15.                 objDSPatients.AcceptChanges()
  16.             Catch eUpdate As System.Exception
  17.                 'Add your error handling code here.
  18.                 Throw eUpdate
  19.             End Try
  20.             'Add your code to check the returned dataset for any errors that may have been
  21.             'pushed into the row object's error.
  22.         End If
  23.  
  24.     End Sub
  25.     Public Sub LoadDataSet()
  26.         'Create a new dataset to hold the records returned from the call to FillDataSet.
  27.         'A temporary dataset is used because filling the existing dataset would
  28.         'require the databindings to be rebound.
  29.         Dim objDataSetTemp As Health1.DSPatients
  30.         objDataSetTemp = New Health1.DSPatients
  31.         Try
  32.             'Attempt to fill the temporary dataset.
  33.             Me.FillDataSet(objDataSetTemp)
  34.         Catch eFillDataSet As System.Exception
  35.             'Add your error handling code here.
  36.             Throw eFillDataSet
  37.         End Try
  38.         Try
  39.             'Empty the old records from the dataset.
  40.             objDSPatients.Clear()
  41.             'Merge the records into the main dataset.
  42.             objDSPatients.Merge(objDataSetTemp)
  43.         Catch eLoadMerge As System.Exception
  44.             'Add your error handling code here.
  45.             Throw eLoadMerge
  46.         End Try
  47.  
  48.     End Sub
  49.     Public Sub UpdateDataSource(ByVal ChangedRows As Health1.DSPatients)
  50.         Try
  51.             'The data source only needs to be updated if there are changes pending.
  52.             If (Not (ChangedRows) Is Nothing) Then
  53.                 'Open the connection.
  54.                 Me.OleDbConnection1.Open()
  55.                 'Attempt to update the data source.
  56.                 OleDbDataAdapter1.Update(ChangedRows)
  57.                 OleDbDataAdapter2.Update(ChangedRows)
  58.                 OleDbDataAdapter3.Update(ChangedRows)
  59.                 OleDbDataAdapter4.Update(ChangedRows)
  60.             End If
  61.         Catch updateException As System.Exception
  62.             'Add your error handling code here.
  63.             Throw updateException
  64.         Finally
  65.             'Close the connection whether or not the exception was thrown.
  66.             Me.OleDbConnection1.Close()
  67.         End Try
  68.  
  69.     End Sub
  70.     Public Sub FillDataSet(ByVal dataSet As Health1.DSPatients)
  71.         'Turn off constraint checking before the dataset is filled.
  72.         'This allows the adapters to fill the dataset without concern
  73.         'for dependencies between the tables.
  74.         dataSet.EnforceConstraints = False
  75.         Try
  76.             'Open the connection.
  77.             Me.OleDbConnection1.Open()
  78.             'Attempt to fill the dataset through the OleDbDataAdapter1.
  79.             Me.OleDbDataAdapter1.Fill(dataSet)
  80.             Me.OleDbDataAdapter2.Fill(dataSet)
  81.             Me.OleDbDataAdapter3.Fill(dataSet)
  82.             Me.OleDbDataAdapter4.Fill(dataSet)
  83.         Catch fillException As System.Exception
  84.             'Add your error handling code here.
  85.             Throw fillException
  86.         Finally
  87.             'Turn constraint checking back on.
  88.             dataSet.EnforceConstraints = True
  89.             'Close the connection whether or not the exception was thrown.
  90.             Me.OleDbConnection1.Close()
  91.         End Try
  92.     End Sub