Dealling With Constraint Errors

  • Thread starter Thread starter OHM
  • Start date Start date
O

OHM

First create a typed-dataset with an integer ID primary-key field and a
string Name field.
Then create a form with a datagrid and a reference to the dataset on
it. set the grids datasource to dataset1.table1.
If you then enter 1 and Me on the first line, all is OK.
If you enter 1 and Me2on the second line, you get the following error:
"Error when committing the row to the original datastore" - "Column ID is
constrianed to be unique, Value 1 is already present. Do you want to correct
the value? Yes/No.
I would like to put my own message up for the user or do something
else, but I cant seem to find a way to hook into this internally-handled
ADO.NET error.
Any answers would be greatfully received.
 
Hi OHM,

From your description, you want to manually handled the Excpetion thrown by
the ADO.NET when validating the input data value and represent its error
message by our custom message, yes?

Based on my research and test, when we input the value in DataGrid and the
Following Event of the DataTable will be fired in Seqence
DataTable.ColumnChanging
DataTable.ColumnChanged.
#DataTable.ColumnChanged Event
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDataDataTableC
lassColumnChangedTopic.asp?frame=true

And the interal error message is sent by ADO.NET after it call its internal
methods( to check primary key or constraint foreign key ....). And all this
validation will start after the DataRow.EndEdit method. And by default this
is called by ADO.NET after the above two event( DataTable.ColumnChanging
and DataTable.ColumnChanged), that make us unable to change the exception
it throws. My solution is to hook the DataTable.ColumnChanged Event and
manually get the current editing DataRow and call its "EndEdit()" method
manually ,that'll cause the new input data be validation at once and we can
catch the thrown internal excpetion if the data is invalidated and
determine whether to continue or cancel the current edit in the "Catch( ){
}" block. The below is the example code I used to test:

private void DataForm_ColumnChanged( object sender,
DataColumnChangeEventArgs e )
{

try
{
// this will cause the ADO.NET checking data validation first and throw
exception if data is invalidated
e.Row.EndEdit();

}
catch(Exception ex)
{
if( .... ) //if necessary, cancel the edit
{
e.Row.CancelEdit();
MessageBox.Show("Our Customer error message");
}
}

}

Please also have a try on your side. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Hi OHM,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top