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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:58:32+00:00 2026-05-13T19:58:32+00:00

From a .NET 3.5 / C# app, I would like to catch SqlException but

  • 0

From a .NET 3.5 / C# app, I would like to catch SqlException but only if it is caused by deadlocks on a SQL Server 2008 instance.

Typical error message is Transaction (Process ID 58) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

Yet, it does not seem to be a documented error code for this exception.

Filtering exception against the presence of the deadlock keyword in their message seems a very ugly way to achieve this behavior. Does someone know the right way of doing this?

  • 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-13T19:58:32+00:00Added an answer on May 13, 2026 at 7:58 pm

    The Microsft SQL Server-specific error code for a deadlock is 1205 so you’d need to handle the SqlException and check for that. So, e.g. if for all other types of SqlException you want the bubble the exception up:

    catch (SqlException ex)
    {
        if (ex.Number == 1205)
        {
            // Deadlock 
        }
        else
            throw;
    }
    

    Or, using exception filtering available in C# 6

    catch (SqlException ex) when (ex.Number == 1205)
    {
        // Deadlock 
    }
    

    A handy thing to do to find the actual SQL error code for a given message, is to look in sys.messages in SQL Server.

    e.g.

    SELECT * FROM sys.messages WHERE text LIKE '%deadlock%' AND language_id=1033
    

    An alternative way to handle deadlocks (from SQL Server 2005 and above), is to do it within a stored procedure using the TRY…CATCH support:

    BEGIN TRY
        -- some sql statements
    END TRY
    BEGIN CATCH
        IF (ERROR_NUMBER() = 1205)
            -- is a deadlock
        ELSE
            -- is not a deadlock
    END CATCH
    

    There’s a full example here in MSDN of how to implement deadlock retry logic purely within SQL.

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

Sidebar

Related Questions

What would be some compelling reasons to upgrade a web app from .NET 1.1
We are about to develop a web app from scratch using ASP.Net, and would
I'm currently writing an ASP.Net app from the UI down. I'm implementing an MVP
I'm creating an asp.net app with just some lite data access from xml files.
From within a DLL that's being called by a C#.NET web app, how do
I am looking at moving my company's internal business app from VB.NET to PHP.
I am from a c# background and am converting a vb.net windows forms app
In the process of updating a web app from ASP to ASP.NET, I want
In .NET, how can I prevent multiple instances of an app from running at
In our .Net CF app we are getting weird errors from parts of the

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.