title: In vb.net, why am I getting a duplicate key error when I have used a revised key?
body: <p>I am using Visual Studio 2015 Community edition and programming in vb.net using ms sql-server 2015. I have set up the data adapter and data table in the form load event. (see below)
          Private m_DABinders As New SqlDataAdapter
      Private m_CBBinders As New SqlCommandBuilder
      Private m_DataTableBinders As New Data.DataTable</p>
  
  <p>I use the code below to insert a row into the Binders table and to check and handle the duplicate key problem. This works fine. My problem comes up when after I get this error, I change the key value to something unique and try to add the row again, and I still get the same duplicate key error, which, strangely enough, shows the original duplicate key value rather than the new one. (I have used MessageBox to ensure that the updated key value is being placed into the appropriate column.) It behaves as if it's still trying to insert the row with the duplicate key before it inserts the updated row.</p>
  
  <p>Do I somehow have to remove the "bad" row from the data adapter or data table after the error? I notice by doing a row count that the rows on the data table goes up by one each time I get this error so I'm thinking I have to delete it somehow but I'm not sure how.</p>
  
  <p>I'm very new to programming in vb.net and with ms sql-server so please don't assume much prior knowledge in your replies. Thanks.</p>
  
  <p>Here is the sub-routine that I use to insert the row.
          Public Sub CreateBinderRow()</p>
  
  <pre><code>    Try
  
          Dim keyValueBinder = tbLocation.Text &amp; tbProject.Text &amp; tbBinder.Text
          Dim drNewBinderRow As DataRow = m_DataTableBinders.NewRow()
  
          drNewBinderRow("KeyValue") = keyValueBinder
          drNewBinderRow("Location") = tbLocation.Text
          drNewBinderRow("ProjectName") = tbProject.Text
          drNewBinderRow("BinderName") = tbBinder.Text
          drNewBinderRow("LastUpdated") = Now
  
          m_DataTableBinders.Rows.Add(drNewBinderRow)
          m_DABinders.Update(m_DataTableBinders)
  
          boolCreateBinderOK = True
  
      Catch dbException As System.Data.SqlClient.SqlException
          boolCreateBinderOK = False
          MessageBox.Show("SQLException: " &amp; dbException.ToString)
          MessageBox.Show("Error creating Binder row - probably duplicate values", "Binder - Error Creating Binder Row",
                          MessageBoxButtons.OK, MessageBoxIcon.Error)
  
      Catch dbException As Exception
          boolCreateBinderOK = False
          MessageBox.Show("SQLException: " &amp; dbException.ToString)
          MessageBox.Show("Error creating Binder row", "Binder - Error Creating Binder Row",
                          MessageBoxButtons.OK, MessageBoxIcon.Error)
  
      End Try
  
  End Sub
  </code></pre>
  
  <p>BTW - The error I am getting is: SqlException (0x80131904) Violation of Primary Key constraint 'PK.Binders' Cannot insert duplicate key in object 'dbo.Binders'. The duplicate key value is (my data). The statement has been terminated.</p>