If I call EndCurrentEdit on a data linked form which contains data that
breaks a unique constraint, a ConstraintException is thrown as expected.
If I catch that exception and call EndCurrentEdit again, it appears to work
but the row is in fact returned to its original state.
It is as though the second call was actually CancelCurrentEdit.
What is the correct way to handle a ConstraintException such that the user
can change what they've entered and try to commit the record again?
Here is some code to reproduce my problem. Create a form and add a single
TextBox to it.
Place the following code into the Form.Load event.
DataTable table = new DataTable("TABLE");
table.Columns.Add("COLUMN");
table.Constraints.Add("CONSTRAINT", table.Columns[0], false);
table.Rows.Add(new object[] {"first"});
table.Rows.Add(new object[] {"second"});
//binding manager points to the first row which contains "first"
textBox1.DataBindings.Add("Text", table, "COLUMN");
//change it to clash with the second row
textBox1.Text = "second";
try {
this.BindingContext[table].EndCurrentEdit();
Console.WriteLine("success");
}
catch(ConstraintException) {
Console.WriteLine("failed");
}
try {
this.BindingContext[table].EndCurrentEdit();
Console.WriteLine("success");
}
catch(ConstraintException) {
Console.WriteLine("failed");
}
You will notice that it failed the first time and succeeded the second.
Nathan