SqlException class does’nt have public constructor



I am currently preparing ado.net 3.5 course so I try to throw SqlException in one of my examples and realize that SqlException class is sealed and has no public constructor.

I have found solution on Rido blog http://blogs.msdn.com/rido how to create SqlException using reflection.

        public static SqlException CreateSqlException(string errorMessage, int errorNumber)

        {

 

            SqlErrorCollection collection = GetErrorCollection();

            SqlError error = GetError(errorNumber, errorMessage);

            MethodInfo addMethod = collection.GetType().

            GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance);

            addMethod.Invoke(collection, new object[] { error });

            Type[] types = new Type[] { typeof(string), typeof(SqlErrorCollection) };

            object[] parameters = new object[] { errorMessage, collection };

            ConstructorInfo constructor = typeof(SqlException).

            GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);

            SqlException exception = (SqlException)constructor.Invoke(parameters);

            return exception;

        }

        private static SqlError GetError(int errorCode, string message)

        {

            object[] parameters = new object[] {

            errorCode, (byte)0, (byte)10, "server", message, "procedure", 0 };

            Type[] types = new Type[] {

            typeof(int), typeof(byte), typeof(byte), typeof(string), typeof(string),

            typeof(string), typeof(int) };

            ConstructorInfo constructor = typeof(SqlError).

             GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);

            SqlError error = (SqlError)constructor.Invoke(parameters);

            return error;

        }

 

        private static SqlErrorCollection GetErrorCollection()

        {

            ConstructorInfo constructor = typeof(SqlErrorCollection).

            GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { }, null);

            SqlErrorCollection collection = (SqlErrorCollection)constructor.Invoke(new object[] { });

            return collection;

        }

  • http://blog.rthand.com/ Miha Markic

    Why would you need to throw a Sql(Server)Exception by yourself? To simulate some behaviour?

  • http://blog.developers.ba/ Radenko Zec

    Thanks for comment Miha

    I try to throw SqlException manually to show some thing about error handling for my Ado.net course.My need for throwing SqlException is only for testing purpose for this course.
    If we write some code to handle all types of SqlExceptions and show user friendly message if some error number occurs we could use this for testing that code?

  • http://blog.rthand.com/ Miha Markic

    I see. Why don’t you create a real test scenario – I mean firing a bunch of SQL things and get real errors? But I guess you know why you are doing it that way :-)

  • Sandy

    Hmmm graet I used it to test a deadlock scenario in my assignment…. I tried all sorts of things to get a real exception from SQL server but all got failed….

  • http://blog.developers.ba/ Radenko Zec

    I am glad I can help :-)