none
How to catch a Command time out exception in c#

    Question

  • Apart from SQLException , is there any other exception precisely for SQl command outs?

    If not, within SQLException how to identify that it is regarding the sqlcommand time out?

    Tuesday, April 21, 2009 11:42 AM

Answers

  • If you look at TdsParser.ProcessNetLibError method (System.Data.dll from .NET 1.0 ) there's a list of error codes. They should be in SqlException.Number property and you can use that.

    (Disclaimer: the code has moved down to native since then, so now I cannot prove it's perfectly ok to rely on Number. On the other hand, in System.Data.dll from .NET 2.0 there's TdsEnums class that declares public const short TIMEOUT_EXPIRED = -2; so most likely nothing has changed fundamentally.)

    Anyway - putting all possible error numbers here, just for the reference.

    -3: ZeroBytes
    -2: Timeout
    -1: Unknown
    1: InsufficientMemory
    2: AccessDenied
    3: ConnectionBusy
    4: ConnectionBroken
    5: ConnectionLimit
    6: ServerNotFound
    7: NetworkNotFound
    8: InsufficientResources
    9: NetworkBusy
    10: NetworkAccessDenied
    11: GeneralError
    12: IncorrectMode
    13: NameNotFound
    14: InvalidConnection
    15: ReadWriteError
    16: TooManyHandles
    17: ServerError
    18: SSLError
    19: EncryptionError
    20: EncryptionNotSupported

    More on this here .
    • Proposed as answer by codevanced Tuesday, April 21, 2009 10:08 PM
    • Marked as answer by Raj_Microsoft Wednesday, April 22, 2009 5:30 AM
    Tuesday, April 21, 2009 10:08 PM

All replies

  • You can check out the Errors Collection property of the SqlException class to see if there's something usefull in there
    Summary: Gets a collection of one or more System.Data.SqlClient.SqlError objects that give detailed information about exceptions generated by the .NET Framework Data Provider for SQL Server. Returns: The collected instances of the System.Data.SqlClient.SqlError class.
    Tuesday, April 21, 2009 1:53 PM
  • If you look at TdsParser.ProcessNetLibError method (System.Data.dll from .NET 1.0 ) there's a list of error codes. They should be in SqlException.Number property and you can use that.

    (Disclaimer: the code has moved down to native since then, so now I cannot prove it's perfectly ok to rely on Number. On the other hand, in System.Data.dll from .NET 2.0 there's TdsEnums class that declares public const short TIMEOUT_EXPIRED = -2; so most likely nothing has changed fundamentally.)

    Anyway - putting all possible error numbers here, just for the reference.

    -3: ZeroBytes
    -2: Timeout
    -1: Unknown
    1: InsufficientMemory
    2: AccessDenied
    3: ConnectionBusy
    4: ConnectionBroken
    5: ConnectionLimit
    6: ServerNotFound
    7: NetworkNotFound
    8: InsufficientResources
    9: NetworkBusy
    10: NetworkAccessDenied
    11: GeneralError
    12: IncorrectMode
    13: NameNotFound
    14: InvalidConnection
    15: ReadWriteError
    16: TooManyHandles
    17: ServerError
    18: SSLError
    19: EncryptionError
    20: EncryptionNotSupported

    More on this here .
    • Proposed as answer by codevanced Tuesday, April 21, 2009 10:08 PM
    • Marked as answer by Raj_Microsoft Wednesday, April 22, 2009 5:30 AM
    Tuesday, April 21, 2009 10:08 PM