DataTable throwing exception on RejectChanges
Posted
by
Vale
on Stack Overflow
See other posts from Stack Overflow
or by Vale
Published on 2012-03-19T09:52:25Z
Indexed on
2012/03/19
10:04 UTC
Read the original article
Hit count: 306
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.
© Stack Overflow or respective owner