PC Review


Reply
Thread Tools Rate Thread

Catching ConstraintExceptions on dialogs and allowing the user to keep trying...

 
 
Nathan Baulch
Guest
Posts: n/a
 
      7th Feb 2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User ID not catching for one not all Jake F Microsoft Access VBA Modules 4 1st May 2009 07:16 PM
Catching ConstraintExceptions on dialogs and allowing the user to keep trying... Nathan Baulch Microsoft Dot NET Framework 0 31st Jan 2005 11:03 PM
Catching ConstraintExceptions on dialogs and allowing the user to keep trying... Nathan Baulch Microsoft ADO .NET 0 28th Jan 2005 03:36 AM
Re: Catching user input Duane Hookom Microsoft Access Reports 0 29th Mar 2004 04:13 PM
User Error Catching Travis Microsoft Dot NET 1 25th Mar 2004 08:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:03 PM.