Foreign key error while deleting a record

0

I have a table with products and another with the category of that 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 another table), that is when a category is associated with a product and gives the error, instead of the user viewing the I want to get an error message on a page or a label, something that the user can see because he can not 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 'CategoryID'.       The statement has been terminated.

    
asked by anonymous 17.07.2014 / 13:20

2 answers

0

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

Web.config file:

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

error page

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

    
19.07.2014 / 14:07
1

You can deal with the error code, 574 in that case (from what I quickly looked here, test there):

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