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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:04:28+00:00 2026-06-12T01:04:28+00:00

I am trying to create a transaction like so: using (TransactionScope scope = new

  • 0

I am trying to create a transaction like so:

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
      options))
{
    try
    {
        dbContext.MyTable.PartnerId = someGuid;
        dbContext.SaveChanges();
        scope.Complete();
        dbContext.AcceptAllChanges()
    }
    catch (Exception ex)
    {
        log.LogMessageToFile("Exception - ExceptionType: " + 
        ex.GetType().ToString() + "Exception Messsage: " + ex.Message);              
    }
}

I know if I try to insert an item manully in sql with a duplicate in a specific column, I get the following error from sql:

Cannot insert duplicate key row in object ‘dbo.MyTable’ with unique index ‘idx_PartnerId_notnull’. The duplicate key value is (7b072640-ca81-4513-a425-02bb3394dfad).

How can I programatically catch this exception specifically, so I can act upon it.

This is the constraint I put on my column:

CREATE UNIQUE NONCLUSTERED INDEX idx_yourcolumn_notnull
ON YourTable(yourcolumn)
WHERE yourcolumn IS NOT NULL;
  • 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-12T01:04:29+00:00Added an answer on June 12, 2026 at 1:04 am

    Try this:

    try {
    }
    catch (SqlException sqlEx) {
    }
    catch (Exception ex) {
    }
    

    SQL errors and warnings that happen on the server side are caught in this exception.
    Read about it here:
    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception(v=vs.110).aspx

    The above answer would allow you to catch the SqlException, but you would need to further refine the handling within the ‘SqlException’ catch block if you only want to inform the user of a particular error. The SqlException class has a property of ‘ErrorCode’ from which you can derive the actual error being produced by the server. Try doing something like below:

    try 
    {
    }
    catch (SqlException sqlEx) 
    {
       if(sqlEx.ErrorCode == 2601)
       {
          handleDuplicateKeyException();
       }
    }
    

    2601 is the actual error code produced by SQL Server for you particular error. For a full list just run the SQL:

    SELECT * FROM sys.messages

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

Sidebar

Related Questions

I'm trying to create a two dimensional array like this... @transactions = {} @expenses.each
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
Trying to create a new Dedicated Cache Role in Windows Azure but get the
I have 3 tables: user_transaction, transaction and users I am trying to create a
Using LINQ-to-SQL, I would like to automatically create child records when inserting the parent
I'm new to Blackberry. I use phonegap to create cross platform applications. I'm trying
I'm trying to create a table using SMO, and further use the SqlBulkCopy object
We are trying to create a text animation like Transition and Zoom on the
I'm trying to create a fade or slide transition using jQuery or CSS (or

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.