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

  • Home
  • SEARCH
  • 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 5938389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:40:43+00:00 2026-05-22T15:40:43+00:00

How can I catch specific exception using c# ? In my database there is

  • 0

How can I catch specific exception using c# ?
In my database there is unique index on some columns.
when user inserts duplicate record this exception has been throw :

Cannot insert duplicate key row in
object ‘dbo.BillIdentity’ with unique
index ‘IX
_BillIdentity’. The
statement has been terminated.

How can I catch this exception?
Currently I am checking using this code :

 catch (Exception ex) {
    if (ex.Message.Contains("Cannot insert duplicate key row in object 'dbo._BillIdentity' with unique index 'IX__BillIdentity")) {
        string ScriptKey = "$(function() {ShowMessage('Error');});";
        ScriptManager.RegisterStartupScript(Page, GetType(), "script", ScriptKey, true);
    }
}

I think its bad smell code.
Is there any better way?

  • 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-22T15:40:44+00:00Added an answer on May 22, 2026 at 3:40 pm

    You haven’t shown the type of exception which is thrown, but you can catch that specific exception type. For example:

    catch (DuplicateKeyException e) {
        ...
    }
    

    It’s possible that there won’t be a specific exception type for just this error – but if you have to catch something fairly general like SqlException you can then look for more details within the class itself. For example in SqlException there’s an Errors property where you can look at more detailed information about each of the (possibly multiple) errors at the database side. Each SqlError then has a Number property which will give the type of error. You can always fall back to the message if you absolutely have to, but you then need to be aware of the possibility of the message changing for different cultures etc.

    Note that if you’re not really handling the exception, you should probably rethrow it:

    catch (SqlException e) {
        if (CheckWeCanHandle(e)) {
            // Mess with the ScriptManager or whatever
        } else {
            throw;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When we can catch an exception like: Violation of UNIQUE KEY constraint 'IX_Product'. Cannot
We know that we can catch any unexpected exception at application level by using
Is there some way I can catch when a method generates a write to
Example int *ptr; *ptr = 1000; can I catch memory access violation exception using
Can you try/catch a stack overflow exception in java? It seems to be throwing
Is there any addon by which I can disable all catch blocks temporarily. I'm
When catching an exception in .NET, you can have as many type-specific exception blocks
I have some python code that binds to a socket. I catch any exception
In Java, we handle exceptions using try catch blocks. I know that I can
I'm using util.control.Exception.catching to convert internal exceptions into an exception type specific to my

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.