I have a problem in C# in how to handle Error in try – catch.
Example I write code for delete record into database but this record has FK into another table. How to handle this?
try
{
// delete record in database
}
catch (Exception ex)
{
int error = // how to get this code exception
}
as per foreign-key-violation
Take a look at:
how-to-handle-db-exception-in-c-sharp
Which will tell you how to precisely locate the foreign key error.
Also look at MSDN Row Deletion including foreign keys
Which explains about setting cascade to on – which is what I believe you are missing.