Ask a questionAsk a question
 

QuestionSqlException State Property

  • Thursday, November 05, 2009 4:43 PMThomas.W. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi!

    Where can I find a description of the State property values in the SqlException?
    I have tried to search Sql Server Books Online and MSDN but can not find the specification of the values anywhere.

    Best regards,
    Thomas

All Replies

  • Friday, November 06, 2009 8:36 AMMichael Aspengren - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    SqlException.State Property
    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception.state.aspx

      "This is a wrapper for the State property of the first SqlError in the Errors property."

    "SqlError.State Property"
    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlerror.state.aspx

    HTH
    //Michael


    This posting is provided "AS IS" with no warranties.
  • Friday, November 06, 2009 12:01 PMThomas.W. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you for replying.

    However this was about the only information I found by my self at MSDN.
    Both pages explain that the State property "Gets a numeric error code from SQL Server that represents an error, warning or "no data found" message."

    But the question I'm searching for an answer to is what the numeric error codes correspond to.
    ex:
    0 = ?
    1 = ??
    2 = ???


    /Thomas
  • Tuesday, November 10, 2009 3:50 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Thomas,

     

     

    Just as Michael posts, "this is a wrapper for the State property of the first SqlError in the Errors property."

    You can use .NET Reflector to get this. When you select SqlException.State, you will get,

    public byte State

    {

        get

        {

            return this.Errors[0].State;

        }

    }

    this.Errors is a SqlErrorCollection. SqlErrorCollection contains an ArrayList of SqlError.

     

    public sealed class SqlError

    {

        // Fields

        private byte errorClass;

        private int lineNumber;

        private string message;

        private int number;

        private string procedure;

        [OptionalField(VersionAdded=2)]

        private string server;

        private string source;

        private byte state;

     

        // Methods

        internal SqlError(int infoNumber, byte errorState, byte errorClass, string server, string errorMessage, string procedure, int lineNumber);

        public override string ToString();

     

        // Properties

        public byte Class { get; }

        public int LineNumber { get; }

        public string Message { get; }

        public int Number { get; }

        public string Procedure { get; }

        public string Server { get; }

        public string Source { get; }

        public byte State { get; }

    }

     

    The yellow part is the actual State you get. If you want to know what does the State mean, call Message property.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Friday, November 13, 2009 2:22 PMThomas.W. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Well yes, but...

    The reason that I want a table with mapping of specific State numbers is that Im writing error handling code for a .NET application working against a SQL Server DB.

    I dont get the impression that the "Message" property is not directly linked to the State property, but to the exception as a whole.


    The serverity property is well explained at this location:
    http://msdn.microsoft.com/en-us/library/ms164086(SQL.90).aspx

    Error codes are explained at:
    http://msdn.microsoft.com/en-us/library/ms165761(SQL.90).aspx
    And additionaly at:
    http://msdn.microsoft.com/en-us/library/ms740668(VS.85).aspx


    But the state property is never explained in a complete context in my opinion.

    Best regards
     / Thomas