I am trying to edit DataTable
Filled by NpgsqlDataAdapter
.
After calling Fill()
method, I have only one row in DataTable
.
This is because DataAdapter
uses Optimistic Concurrency by default. This means that if you are trying to update a row that no longer exists in the database or changed, the update from the DataAdapter
will fail with the exception above.
Possible scenarios:
For example:
DataTable
that will be used for the update.Code = 1101
(for example) directly from the database, i.e. you do not use the DataTable
here. This is emulating another user deleting the row with Code = 1101
from another application. Or some other part in your code deleting the row with Code = 1101
.Code = 1101
from the DataTable
, this is just to show that it is still there even though you have deleted it from the database itself.Quantity
column in the row with Code = 1101
in the DataTable
. This has to be done, otherwise the call to Update will ignore this row when updating.If you want to implement Last Writer Wins, Add the following code:
cb.ConflictOption = ConflictOption.OverwriteChanges;
Also there is one more possible thing : if you have Decimal
/numeric
as columns in the DB they may cause this error even though the data looks the same. This is due to a decimal rounding error.
An important note: You should always use parameterized queries by the way. This kind of string concatenations are open for SQL Injection.