Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here is the code, any ideas why I get this error?

private SQLiteDataAdapter DA_Webfiles;
// Setup connection, fill dataset etc

DataTable dt = this.dataSet.Tables["WEBFILES"];
DataRow newRow = dt.NewRow();
newRow["PATH"] = _url;
dt.Rows.Add(newRow);
this.DA_Webfiles.Update(this.dataSet, "WEBFILES");
// Works to Here

newRow["CONTENT_TYPE"] = "Test Content Type";
this.DA_Webfiles.Update(this.dataSet, "WEBFILES");
// Get ERROR here - Concurrency violation: the UpdateCommand affected 0 of the expected 1 records
like image 374
Greg Avatar asked Oct 21 '09 07:10

Greg


People also ask

What is concurrency violation?

(The database does have a primary key.) The concurrency violation occurs when I run the project, add a record and save it, make a change to the same record and then save it. The full error message reads: Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.

How do you handle concurrency violations?

One way to catch optimistic concurrency violations is to use the SqlDataAdapter's RowUpdated event. This event is fired just after an update attempt, and it fires for each row. Handle this event and check to see if the row was updated by finding out how many rows were affected.

What is a concurrency exception?

Concurrency exceptions (System. Data. DBConcurrencyException) are raised when two users attempt to change the same data in a database at the same time.


1 Answers

you need: dataAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;

got code clue here: Retrieving Identity or Autonumber Values (ADO.NET)

the table:

CREATE TABLE [emp] (
[emp_id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,
[emp_firstname] VARCHAR(100) NOT NULL,
[emp_lastname] varchar(100) not null
)

the code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();


        var c = Connect();

        var da = new SQLiteDataAdapter("select emp_id, emp_firstname, emp_lastname from emp where 1 = 0", c);



        var b = new SQLiteCommandBuilder(da);

        da.InsertCommand = new SQLiteCommand(
            @"insert into emp(emp_firstname, emp_lastname ) values(:_emp_firstname, :_emp_lastname);
            select emp_id /* include rowversion field here if you need */ from emp where emp_id = last_insert_rowid();", c);
        da.InsertCommand.Parameters.Add("_emp_firstname", DbType.String, 0, "emp_firstname");
        da.InsertCommand.Parameters.Add("_emp_lastname", DbType.String, 0, "emp_lastname");
        da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;

        da.UpdateCommand = b.GetUpdateCommand();
        da.DeleteCommand = b.GetDeleteCommand();


        var dt = new DataTable();
        da.Fill(dt);

        var nr = dt.NewRow();
        nr["emp_firstname"] = "john";
        nr["emp_lastname"] = "lennon";

        dt.Rows.Add(nr);

        da.Update(dt);

        dt.AcceptChanges();

        nr["emp_lastname"] = "valjean";
        da.Update(dt);

    }

    SQLiteConnection Connect()
    {
        return new SQLiteConnection(@"Data Source=../../test.s3db;Version=3;");
    }
}

the code above works on multi-insert too. proof-of-concept code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();


        var c = Connect();

        var da = new SQLiteDataAdapter("select emp_id, emp_firstname, emp_lastname from emp where 1 = 0", c);



        var b = new SQLiteCommandBuilder(da);

        da.InsertCommand = new SQLiteCommand(
            @"insert into emp(emp_firstname, emp_lastname ) values(:_emp_firstname, :_emp_lastname);
            select emp_id /* include rowversion field here if you need */ from emp where emp_id = last_insert_rowid();", c);
        da.InsertCommand.Parameters.Add("_emp_firstname", DbType.String, 0, "emp_firstname");
        da.InsertCommand.Parameters.Add("_emp_lastname", DbType.String, 0, "emp_lastname");
        da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;

        da.UpdateCommand = b.GetUpdateCommand();
        da.DeleteCommand = b.GetDeleteCommand();


        var dt = new DataTable();
        da.Fill(dt);

        var nr = dt.NewRow();
        nr["emp_firstname"] = "john";
        nr["emp_lastname"] = "lennon";


        var nrx = dt.NewRow();
        nrx["emp_firstname"] = "paul";
        nrx["emp_lastname"] = "mccartney";


        dt.Rows.Add(nr);
        dt.Rows.Add(nrx);

        da.Update(dt);

        dt.AcceptChanges();


        nrx["emp_lastname"] = "simon";
        da.Update(dt);

        nr["emp_lastname"] = "valjean";
        da.Update(dt);

    }

    SQLiteConnection Connect()
    {
        return new SQLiteConnection(@"Data Source=../../test.s3db;Version=3;");
    }
}
like image 111
Michael Buen Avatar answered Nov 15 '22 17:11

Michael Buen