Catch "Cannot insert duplicate key row" exception

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I really don't like the users getting an unhandled expception page,
and I'm still to new with ASP.Net and C#. So please accept my
appology for the 2 part question.

SqlException (0x80131904)

1.) Is an "Cannot insert duplicate key row" exception from a FormView
returned as part of the ItemInserting or ItemInserted event?

2.) What is the recommended way to catch and deal with an "Cannot
insert duplicate key row " exception?

I look at dealing with Exceptions as an Art, and I have a lot of
respect for those that can do it with very little effort. I hope that
one of you will be able to help me with this.

Thanks
 
Dave said:
I really don't like the users getting an unhandled expception page,
and I'm still to new with ASP.Net and C#. So please accept my
appology for the 2 part question.

SqlException (0x80131904)

1.) Is an "Cannot insert duplicate key row" exception from a FormView
returned as part of the ItemInserting or ItemInserted event?

2.) What is the recommended way to catch and deal with an "Cannot
insert duplicate key row " exception?

I look at dealing with Exceptions as an Art, and I have a lot of
respect for those that can do it with very little effort. I hope that
one of you will be able to help me with this.

You page redirect or ServerTransfer to an error page and display the
message, and you come out of the application gracefully or redirect to a
Logon page as an example.

If you have some kind of problem with inserting records into a SQL Server
table based on a table's primary record-key, you got other problems out the
gate that you should be dealing with, because this kind of error condition
should net be happening if the code is solid.
 
Mr. Arnold,
Thanks for the quick reply. I understand what you're saying, and as
far as my code being solid, that's why I'm trying to capturing the
duplicate entry error. Redirecting the page once the specific error
above is caught isn't the issue. The issue is how do I capture the
specific SqlException (0x80131904) error?

Also...

1.) Is an "Cannot insert duplicate key row" exception from a
FormView
returned as part of the ItemInserting or ItemInserted event?

Thanks, and I hope to hear from you soon.
 
Hi Dave,

The exception occurs between these events, when the data is attempted inserted in the datagrid's datasource, but if the exception occures, you should be able to see it in the Exception property of the DetailsViewInsertedEventArgs object. This object also has a ExceptionHandled property enabling you to consume the exception.
 
Dave said:
Mr. Arnold,
Thanks for the quick reply. I understand what you're saying, and as
far as my code being solid, that's why I'm trying to capturing the
duplicate entry error. Redirecting the page once the specific error
above is caught isn't the issue. The issue is how do I capture the
specific SqlException (0x80131904) error?

An Exception I know has Innertext messages you might be able to parce. You
should look at the properties of the Exception.

catch (SqlException sqlex)

sqlex.Innertext or something like that -- look at the Exception properties
to see what you can use to trap on for a specific error.
1.) Is an "Cannot insert duplicate key row" exception from a
FormView
returned as part of the ItemInserting or ItemInserted event?

I cannot tell you about that one, because I never allow the UI/ASP page to
make direct contact with a database.
 
The code to catch this is really pretty easy. The key is to just re-throw it
if it's not the error you're looking for.

try
{
bla bla
}
catch(SqlException ex)
{
if (!ex.ErrorCode == 0x80131904)
throw;

// Put relevant error handling logic here
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top