Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 04, 2010 06:26 PM by MyronCope
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
Cannot insert duplicate key row in ojbect "dbo.someTableName" with unique index.....The statement has been terminated
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
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:
// attempt some kind of update
if (e.Message.StartsWith("Violation of UNIQUE KEY constraint"))
// handle your violation
// re-throw the error if you haven't handled it
running this through a vb converter yields:
' attempt some kind of update
Catch e As Exception
If e.Message.StartsWith("Violation of UNIQUE KEY constraint") Then
' handle your violation
' re-throw the error if you haven't handled it
May 04, 2010 03:54 PM|LINK
check out this post from Frederick..should do the trick.
May 04, 2010 05:36 PM|LINK
I am trying this in both gridview and sqlDataSource events but not seeing it being fired, which event would you try this in?
May 04, 2010 06:26 PM|LINK
you know what, I found what event to handle this in:
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.....