I found this error when working with a DataTable. I added a primary key column to the DataTable, than added one row to this table, deleted this row and added a row with the same key to the table. It works. When I tried to call RejectChanges() on it, I got a ConstraintException , saying that this value is already present. Here is an example:
var dataTable = new DataTable(); var column = new DataColumn("ID", typeof(decimal)); dataTable.Columns.Add(column); dataTable.PrimaryKey = new [] {column }; decimal id = 1; var oldRow = dataTable.NewRow(); oldRow[column] = id; dataTable.Rows.Add(oldRow); dataTable.AcceptChanges(); oldRow.Delete(); var newRow = dataTable.NewRow(); newRow[column] = id; dataTable.Rows.Add(newRow); dataTable.RejectChanges();
I think that since the row is deleted, the exception should not be thrown (the restriction is not violated because the row is in the delete state). Is there anything I can do about it? Any help is appreciated.
source share