470,594 Members | 1,162 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

Catch "Cannot insert duplicate key row" exception

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

Nov 4 '07 #1
6 11698

"Dave" <Da**********@Jacobs.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
>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.
Nov 4 '07 #2
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.

On Nov 4, 7:13 am, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:
"Dave" <Dave.Burk...@Jacobs.comwrote in message

news:11**********************@i13g2000prf.googlegr oups.com...


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.- Hide quoted text -

- Show quoted text -

Nov 4 '07 #3
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.
On Sun, 04 Nov 2007 14:26:07 +0100, Dave <Da**********@Jacobs.comwrote:
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.

On Nov 4, 7:13 am, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:
>"Dave" <Dave.Burk...@Jacobs.comwrote in message

news:11**********************@i13g2000prf.googleg roups.com...


>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.- Hide quoted text -

- Show quoted text -




--
Happy coding!
Morten Wennevik [C# MVP]
Nov 4 '07 #4

"Dave" <Da**********@Jacobs.comwrote in message
news:11**********************@v29g2000prd.googlegr oups.com...
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.
Nov 5 '07 #5
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
}

--
Chris Mullins

"Dave" <Da**********@Jacobs.comwrote in message
news:11**********************@v29g2000prd.googlegr oups.com...
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.

On Nov 4, 7:13 am, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:
>"Dave" <Dave.Burk...@Jacobs.comwrote in message

news:11**********************@i13g2000prf.googleg roups.com...


>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.- Hide quoted text -

- Show quoted text -


Nov 5 '07 #6
Chirs, Would that be under the Inserted Exception?

On Nov 5, 3:25 pm, "Chris Mullins [MVP - C#]" <cmull...@yahoo.com>
wrote:
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

}

--
Chris Mullins

"Dave" <Dave.Burk...@Jacobs.comwrote in message

news:11**********************@v29g2000prd.googlegr oups.com...
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.
On Nov 4, 7:13 am, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:
"Dave" <Dave.Burk...@Jacobs.comwrote in message
>news:11**********************@i13g2000prf.googleg roups.com...
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.- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -

Nov 8 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Thomas Barnet-Lamb | last post: by
1 post views Thread by Andreas Poller | last post: by
1 post views Thread by news.microsoft.com | last post: by
1 post views Thread by Mary Kerrigan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.