I am trying to edit a DataTable Filled with NpgsqlDataAdapter . After calling the Fill() method, I have only one row in the DataTable . Then I changed the value of only one column and tried to update as shown below.

Then I get this error:
DBConcurrencyException error occurred
My code is:
NpgsqlDataAdapter getAllData = new NpgsqlDataAdapter("SELECT sn, code,product, unitprice, quantity, InvoiceNo, Date FROM stocktable WHERE Code='" + product + "' ORDER BY EDate ASC", DatabaseConnectionpg); DataTable ds1 = new DataTable(); ds1.Clear(); getAllData.Fill(ds1); if (ds1.Rows.Count > 0) { ds1.Rows[0]["Quantity"] = qty;// calculated value } ds1 = ds1.GetChanges(); NpgsqlCommandBuilder cb = new NpgsqlCommandBuilder(getAllData); //getAllData.RowUpdating += (sender2, e2) => { e2.Command.Parameters.Clear(); }; //cb.SetAllValues = false; getAllData.DeleteCommand = cb.GetDeleteCommand(); getAllData.InsertCommand = cb.GetInsertCommand(); getAllData.UpdateCommand = cb.GetUpdateCommand(); int x = getAllData.Update(ds1); if (x > 0) { ds1.AcceptChanges(); }
EDIT: I have three fields as primary keys, and I only call two fields in a select statement. Is this the reason for DBConcurrency error? But I can update a table with the same parameters (three fields as primary key) in SQL Server 2005.
UPDATE:
I found a solution, and a solution I created and used a second DataAdapter to update the data. I used getAllData strong> (NpgSqlDataAdapter). To populate the table as
NpgsqlDataAdapter getAllData = new NpgsqlDataAdapter("SELECT code,product, unitprice, quantity, InvoiceNo, Date FROM stocktable WHERE Code='" + product + "' ORDER BY EDate ASC", DatabaseConnectionpg);
And also created the following adapter to update as
NpgsqlDataAdapter updateadap= new NpgsqlDataAdapter("SELECT sn, quantity FROM stocktable WHERE Code='" + product + "' ORDER BY EDate ASC", DatabaseConnectionpg); NpgsqlCommandBuilder cb = new NpgsqlCommandBuilder(updateadap); //getAllData.RowUpdating += (sender2, e2) => { e2.Command.Parameters.Clear(); }; //cb.SetAllValues = false; updateadap.DeleteCommand = cb.GetDeleteCommand(); updateadap.InsertCommand = cb.GetInsertCommand(); updateadap.UpdateCommand = cb.GetUpdateCommand(); int x = updateadap.Update(ds1); if (x > 0) { ...... }
I tried a lot and found that NpgsqlDataAdapter was having a problem with the column code. When I overshadowed this, it worked. The column code's DataType is varchar. I do not know why this happened. Does anyone have an idea?