Foreign Key error when deleting a record

Asked

Viewed 306 times

0

I have a table with products and another one with the category of this product. In the category table I have a detailsView with a delete button, and it deletes when a category is not associated with a product (from the other table), that is when a category is associated with a product and gives the error, instead of the user viewing the error page, I want an error warning to appear on a page or label, anything the user understands because they cannot delete the category.

error:

The DELETE statement conflicted with the REFERENCE Constraint "Fk_products_categories". The Conflict occurred in database "C: USERS DOCUMENTS VISUAL STUDIO 2013 WEBSITES PROJECT APP_DATA DBTI2.MDF", table "dbo.Products", column 'Idcategoria'. The statement has been terminated.

  • 1

    Since you want us to do your job, you’ll give a portion of your salary to whoever does it?

  • It’s a legitimate question from a junior programmer. If you don’t want to help just ignore.

2 answers

1

You can treat by error code, 574 in this case (by what I looked quickly here, test there):

try
{
   // seu comando...
}
catch (SqlException ex)
{
    if (ex.Number == 574)
    {
        // Seu tratamento
    }
    throw;
}

0


Error 574 I think does not exist. I solved the problem in this way:

Web file.config:

<customErrors mode="On">
    <error statusCode="500" redirect="~/ErrorPages/Oopss.aspx" />   
  </customErrors>

error page

ErrorPage="~/ErrorPages/Oopss.aspx" in the @page directive

Browser other questions tagged

You are not signed in. Login or sign up in order to post.