2 Replies - 3133 Views - Last Post: 27 September 2012 - 07:55 AM Rate Topic: -----

#1 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Concurrency violation: the UpdateCommand affected 0 of the expected 1

Posted 26 September 2012 - 06:49 PM

Good Day. I need some help with vb 2008. I'm using, as have said, vb 2008 and access database. my problem is that i'm getting this error

Quote

Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
every time i add another record. Just to add, im using a binding navigator.
This is the scenario:
I run the program and add record then save (on first try, i does saved the record).
I go another tab and delete a record (still, this works fine).
I go back to the same tab where i can add record and adds again and this is where my problems starts and receive the error message mentioned above.

here is the code i'm using for adding record:
Add Button.
  Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        DetailsBindingNavigatorSaveItem.Enabled = True
        pnlMain.Enabled = True
    End Sub



Save Button.
Private Sub DetailsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DetailsBindingNavigatorSaveItem.Click
        Try
            Me.Validate()
            Me.DetailsBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DataSet1)
            MsgBox("Record Successfully Saved")

            TitleTextBox.Text = ""
            DirectorTextBox.Text = ""
            YRTextBox.Text = ""
            StarringTextBox.Text = ""
            PlotTextBox.Text = ""
            CoverPictureBox.Image = Nothing

            DetailsBindingNavigatorSaveItem.Enabled = False
            pnlMain.Enabled = False
        Catch ex As Exception
            MsgBox(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub




add here is the code i'm using in deleting records:
Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Try
            If MessageBox.Show("Are you sure you want to delete the current record?", _
                               "Confirm Delete", _
                               MessageBoxButtons.OKCancel, _
                               MessageBoxIcon.Question, _
            MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
                Me.DetailsBindingNavigator2.BindingSource.RemoveCurrent()
            End If
            Me.Validate()
            Me.DetailsBindingSource2.EndEdit()
            Me.TableAdapterManager2.UpdateAll(Me.DataSet3)
            MsgBox("Record Successfully Removed")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Concurrency violation: the UpdateCommand affected 0 of the expected 1

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Concurrency violation: the UpdateCommand affected 0 of the expected 1

Posted 27 September 2012 - 07:32 AM

Is the item, you want to add the 2nd time (again), exactly the same record as you added the 1st time, or do you input new data with the same values as 1st time? Are you using DataGridView?
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: Concurrency violation: the UpdateCommand affected 0 of the expected 1

Posted 27 September 2012 - 07:55 AM

OK, this is hard to explain but I'll try:
You have a dataset (disconnected data of your query result)

You use that dataset to populate your grids (add & delete)

You delete a row and pass that on to remove it from the database but your dataset still thinks it's there. When you go back to add in your other grid, the concurrency violation occurs because the disconnected data is trying to refresh a row that doesn't exist in your database.

You can either ensure that you re-fill your dataset with the updated table information prior to allowing add or inform the user and let them decide. Either way, your disconnected data has found that it no longer matches the table and doesn't know what to do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1