Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15917

VS 2019 [RESOLVED] Concurrency violation: the DeleteCommand affected 0 of the expected 1

$
0
0
Hi,

I've searched over tens of threads about this common error but I can't find out where the error is in my code.

I have a datagridview with a bindingsource and the data are stored in an access database.

The insert and update commands works fine but when I insert new data, save to database, then immediately delete the newly added rows, as soon as I try to save, the concurrency violation appears...

Here's my code:

Code:

Private Sub ConfigureDataAdapter()
        Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\DJSOC\DRUGS\CRU\DORA\DORAInv.accdb")
        'Check if the table for this year exists, if not create it
        connection.Open()
        Dim dbSchema As DataTable = connection.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, INV, "TABLE"})
        If dbSchema.Rows.Count = 0 Then
            Dim cmd As New OleDbCommand($"CREATE TABLE [{INV}] ([ID] COUNTER PRIMARY KEY, [IDINT] INTEGER, [ORDER] INTEGER, [NUM] TEXT(50), [DESC] MEMO, [SAMPLES] TEXT(50), [SAMPLEST] TEXT(50))", connection)
            cmd.ExecuteNonQuery()
        End If
        connection.Close()
        'Create TableAdapter
        INVENTORYTableAdapter = New OleDbDataAdapter($"SELECT ID, IDINT, ORDER, NUM, DESC, SAMPLES, SAMPLEST FROM {INV} WHERE IDINT = {IntNum}", connection)
        'Create insert command
        Dim insert As New OleDbCommand($"INSERT INTO {INV} ([IDINT], [ORDER], [NUM], [DESC], [SAMPLES], [SAMPLEST]) VALUES (@IDINT, @ORDER, @NUM, @DESC, @SAMPLES, @SAMPLEST)", connection)
        With insert.Parameters
            .Add("@IDINT", OleDbType.Integer, 4, "IDINT")
            .Add("@ORDER", OleDbType.Integer, 4, "ORDER")
            .Add("@NUM", OleDbType.VarChar, 50, "NUM")
            .Add("@DESC", OleDbType.LongVarChar, 5000, "DESC")
            .Add("@SAMPLES", OleDbType.VarChar, 50, "SAMPLES")
            .Add("@SAMPLEST", OleDbType.VarChar, 50, "SAMPLEST")
        End With
        'Create update command
        Dim update As New OleDbCommand($"UPDATE {INV} SET [IDINT] = @IDINT, [ORDER] = @ORDER, [NUM] = @NUM, [DESC] = @DESC, [SAMPLES] = @SAMPLES, [SAMPLEST] = @SAMPLEST WHERE ID = @ID", connection)
        With update.Parameters
            .Add("@IDINT", OleDbType.Integer, 4, "IDINT")
            .Add("@ORDER", OleDbType.Integer, 4, "ORDER")
            .Add("@NUM", OleDbType.VarChar, 50, "NUM")
            .Add("@DESC", OleDbType.LongVarChar, 5000, "DESC")
            .Add("@SAMPLES", OleDbType.VarChar, 50, "SAMPLES")
            .Add("@SAMPLEST", OleDbType.VarChar, 50, "SAMPLEST")
            .Add("@ID", OleDbType.Integer, 4, "ID")
        End With
        'Create delete command
        Dim delete As New OleDbCommand($"DELETE FROM {INV} WHERE ID = @ID", connection)
        delete.Parameters.Add("@ID", OleDbType.Integer, 4, "ID")
        With INVENTORYTableAdapter
            .MissingSchemaAction = MissingSchemaAction.AddWithKey
            .InsertCommand = insert
            .UpdateCommand = update
            .DeleteCommand = delete
        End With
    End Sub

    Private Sub LoadData()
        INVENTORYTableAdapter.Fill(INVENTORYDataTable)
        INVENTORYBindingSource.DataSource = INVENTORYDataTable
        dgvInventory.DataSource = INVENTORYBindingSource
    End Sub

    Private Sub frmInventory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ConfigureDataAdapter()
        LoadData()
    End Sub

And this is where I save the data:

Code:

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        'Save
        INVENTORYBindingSource.EndEdit()
        INVENTORYTableAdapter.Update(INVENTORYDataTable)
    End Sub

I guess there's a problem with the ID which is autoincrement and generated when the data are saved to the database...

Thanks

Viewing all articles
Browse latest Browse all 15917

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>