I found this bug while working with a DataTable.
I added a primary key column to a DataTable, than added one row to that table, removed that row, and added row with the same key to the table. This works. When I tried to call RejectChanges()
on it, I got ConstraintException
saying that value is already present.
Here is the 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(); // This is where it crashes
I think since the row is deleted, exception should not be thrown (constraint is not violated because row is in deleted state). Is there something I can do about this? Any help is appreciated.
I assume that this has the same cause than following bug issue since the first that will be rejected is your
delete
action:DataTable.RejectChanges() should rollback rows in reverse order
Two possible workarounds:
Disables constrains so the rollback can be done. Reenables constrains after that.
You could use LINQ-to-DataSet to define your own "rollback-order":
Here's the way you "disable" constraints on a
DataTable
temporarily:You can avoid this by using
unique
property of column tortrue
.i.e.
column.Unique = true;
As soon as this property is changed to true, a unique constraint will be created on this column to make sure that values are unique.