Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:50:48+00:00 2026-06-10T21:50:48+00:00

I have a problem in C# in how to handle Error in try –

  • 0

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      
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:50:50+00:00Added an answer on June 10, 2026 at 9:50 pm
    try
    {
        # SQL Stuff
    }
    catch (SqlException ex)
    {
        if (ex.Errors.Count > 0) // Assume the interesting stuff is in the first error
        {
            switch (ex.Errors[0].Number)
            {
                case 547: // Foreign Key violation
                    throw new InvalidOperationException("Some helpful description", ex);
                    break;
                case 2601: // Primary key violation
                    throw new DuplicateRecordException("Some other helpful description", ex);
                    break;
                default:
                    throw new DataAccessException(ex);
            }
        }
    
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with my code. How can I handle an error from
I have a problem with a script and I don't know how to handle
I have a problem with NCurses ... i need to handle all keys like
I'm trying to get a handle on whether we have a problem in our
I have designed a custom section handler before but I'm faced with a problem
I don't have this problem on my local development environment, but I just deployed
First of all - I don't have a problem with bad-indentated code and I
I have a problem with request POST/GET in Android. I am trying to handle
I have a problem with a service, when I try to install it like
I have a problem converting a 32-bit to 64-bit code using the lsaapi.pas unit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.