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 663551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:30:07+00:00 2026-05-13T23:30:07+00:00

I want to tell the user that a record was not deleted because it

  • 0

I want to tell the user that a record was not deleted because it has child data, but how can I be sure that the exception was thrown because of a foreign key violation? I see that there a sqlexception class that is used for all sql 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-05-13T23:30:07+00:00Added an answer on May 13, 2026 at 11:30 pm

    Assume you’re using SQL Server.

    Using teh web archive – https://web.archive.org/web/20190120182351/https://blogs.msdn.microsoft.com/tomholl/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way/

    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);
            }
        }
    
    }
    

    Case 547 is your man.

    UPDATE The above is sample code and should not be used. Please follow the link as to explain why.

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

Sidebar

Related Questions

I want to tell the user they can't modify a textarea that's been disabled
Our application will store some information from a user that we do not want
Basically I will tell you what I want to do. 1: If a user
I want to tell a java client request not to care about the remote
I want to know if I can tell what appdomain a object was created
hello for my app I need to save a record that the user type,
I want to record the client that browse my web page, So I do
I want to develop an application that allow user to writes and print cheques.
I want to tell git to ignore e.g. jpg files regardless of how the
I want to tell whether two tarball files contain identical files, in terms of

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.