Connecting Tech Pros Worldwide Forums | Help | Site Map

Catching a Constraint Exception

clickon
Guest
 
Posts: n/a
#1: Mar 29 '06
I am creating a "setup" page that allows users to fill out various tables of
key,value pairs that are basically the table for foreign keys within a master
table. It all works very well, but when a user tries to delete a record that
is currently being used in the master table the SQLServer obviously won't let
you because of the contraint on hte foreign key and ASP.net throws and
exception. What is the best way to catch this exception, perhaps in the
deleting event of the gridview control that i am using for editing/deleting
data in the table.

Flinky Wisty Pomm
Guest
 
Posts: n/a
#2: Mar 29 '06

re: Catching a Constraint Exception


There's an example of handling SqlDataSource exceptions here
http://fredrik.nsquared2.com/viewpost.aspx?PostID=300 courtesy of the
ever-helpful Fredrik Norméns.

ObjectDataSources work in the same way.

If you want much more control than that, you're getting into murky
waters. Personally, I'd return an error code from my stored proc and
throw a custom exception in the data layer.

clickon
Guest
 
Posts: n/a
#3: Mar 29 '06

re: Catching a Constraint Exception


Thanks that's very helpfull.

To get som ething going i have written the following code

protected void sdsComplaintStatus_Deleted(object sender,
SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
e.ExceptionHandled = true;
lblNoDelete.Text = "Cannot delete, value in use";
lblNoDelete.ForeColor = System.Drawing.Color.Red;
}

}

On the basis that the only exception likely to be thrown is the user is
trying to delete a record that is in use as a foreign key in another table,
and i can make it more sophisticated once i have this up and running. Thing
is it still thorws the exception. Any ideas ?

I have written code to check for the condition before attempting the delete,
but it is a bit complex and this way would be more elegant.



"Flinky Wisty Pomm" wrote:
[color=blue]
> There's an example of handling SqlDataSource exceptions here
> http://fredrik.nsquared2.com/viewpost.aspx?PostID=300 courtesy of the
> ever-helpful Fredrik Norméns.
>
> ObjectDataSources work in the same way.
>
> If you want much more control than that, you're getting into murky
> waters. Personally, I'd return an error code from my stored proc and
> throw a custom exception in the data layer.
>
>[/color]
clickon
Guest
 
Posts: n/a
#4: Mar 29 '06

re: Catching a Constraint Exception


ignore me i was being a dumb ass, the code works fine.

"Flinky Wisty Pomm" wrote:
[color=blue]
> There's an example of handling SqlDataSource exceptions here
> http://fredrik.nsquared2.com/viewpost.aspx?PostID=300 courtesy of the
> ever-helpful Fredrik Norméns.
>
> ObjectDataSources work in the same way.
>
> If you want much more control than that, you're getting into murky
> waters. Personally, I'd return an error code from my stored proc and
> throw a custom exception in the data layer.
>
>[/color]
Flinky Wisty Pomm
Guest
 
Posts: n/a
#5: Mar 29 '06

re: Catching a Constraint Exception


Heh... I did mean to ask "did you wire the event up?" but ... Glad it
works for you.

clickon
Guest
 
Posts: n/a
#6: Mar 29 '06

re: Catching a Constraint Exception


Yeah i did wire the event up, but then saved the codebehind page and forgot
to save the aspx page. Doh!

"Flinky Wisty Pomm" wrote:
[color=blue]
> Heh... I did mean to ask "did you wire the event up?" but ... Glad it
> works for you.
>
>[/color]
Closed Thread