I have a unique constraint setup to prevent users from entering duplicate email addresses in my online system.
When a user tries to edit an existing email to one that already exists (add a duplicate) it shows the following
<ERROR>
Cannot insert duplicate key row in ojbect "dbo.someTableName" with unique index.....The statement has been terminated
</ERROR>
This prevents the user from adding duplicates which is good but I would like to provide a more user friendly exception message and I do not want to show the table and field names to the user.
I am using a SQLDataSource to update the records.
The update statement is set in the .aspx page but I am thinking about manually doing the update in the "SqlDataSource1_Updating" code-behind event.
Does this make sense? If yes, what is the syntax? thanks
Annoyingly there isn't actually an error code which you can capture to handle this error.
If you are running an english based system you can capture the generic sql exception and then inspect the inner message to ensure its what you expect.
This is suseptible to:
the server being deployed on a foreign language server
the error message text being changed at some point in the future
The c# code to handle this error looks like this:
try
{
// attempt some kind of update
}
catch(Exception e)
{
if (e.Message.StartsWith("Violation of UNIQUE KEY constraint"))
{
// handle your violation
}
else
{
// re-throw the error if you haven't handled it
throw;
}
}
running this through a vb converter yields:
Try
' attempt some kind of update
Catch e As Exception
If e.Message.StartsWith("Violation of UNIQUE KEY constraint") Then
' handle your violation
Else
' re-throw the error if you haven't handled it
Throw
End If
End Try
Marked as answer by MyronCope on May 04, 2010 06:27 PM
you know what, I found what event to handle this in:
grdvMyGridView_RowUpdated
It works for now. You are right about possible changing message. It is very strange that there is not some kind of exception code that I can catch.....
MyronCope
Participant
1656 Points
1345 Posts
Capture unique constraint exception and provide user-friendly message
May 04, 2010 03:31 PM|LINK
Using vb.net/asp.net 2005 and SQL Server 2005:
I have a unique constraint setup to prevent users from entering duplicate email addresses in my online system.
When a user tries to edit an existing email to one that already exists (add a duplicate) it shows the following
<ERROR>
Cannot insert duplicate key row in ojbect "dbo.someTableName" with unique index.....The statement has been terminated
</ERROR>
This prevents the user from adding duplicates which is good but I would like to provide a more user friendly exception message and I do not want to show the table and field names to the user.
I am using a SQLDataSource to update the records.
The update statement is set in the .aspx page but I am thinking about manually doing the update in the "SqlDataSource1_Updating" code-behind event.
Does this make sense? If yes, what is the syntax? thanks
rtpHarry
All-Star
56620 Points
8958 Posts
Re: Capture unique constraint exception and provide user-friendly message
May 04, 2010 03:52 PM|LINK
Annoyingly there isn't actually an error code which you can capture to handle this error.
If you are running an english based system you can capture the generic sql exception and then inspect the inner message to ensure its what you expect.
This is suseptible to:
The c# code to handle this error looks like this:
try { // attempt some kind of update } catch(Exception e) { if (e.Message.StartsWith("Violation of UNIQUE KEY constraint")) { // handle your violation } else { // re-throw the error if you haven't handled it throw; } }running this through a vb converter yields:
Try ' attempt some kind of update Catch e As Exception If e.Message.StartsWith("Violation of UNIQUE KEY constraint") Then ' handle your violation Else ' re-throw the error if you haven't handled it Throw End If End Trydliabenow
Member
92 Points
16 Posts
Re: Capture unique constraint exception and provide user-friendly message
May 04, 2010 03:54 PM|LINK
check out this post from Frederick..should do the trick.
http://forums.asp.net/t/859207.aspx
MyronCope
Participant
1656 Points
1345 Posts
Re: Capture unique constraint exception and provide user-friendly message
May 04, 2010 05:36 PM|LINK
thanks Harry,
I am trying this in both gridview and sqlDataSource events but not seeing it being fired, which event would you try this in?
MyronCope
Participant
1656 Points
1345 Posts
Re: Capture unique constraint exception and provide user-friendly message
May 04, 2010 06:26 PM|LINK
you know what, I found what event to handle this in:
grdvMyGridView_RowUpdated
It works for now. You are right about possible changing message. It is very strange that there is not some kind of exception code that I can catch.....