Linked In Twitter RSS

SqlException class does'nt have public constructor

8. March 2009  · Comments (5)

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;

        }

Comments (5) -

Miha Markic
Miha Markic Slovenia
3/6/2009 10:25:02 PM

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

Radenko Zec
Radenko Zec Bosnia and Herzegovina
3/6/2009 11:00:05 PM

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?

Miha Markic
Miha Markic Slovenia
3/7/2009 3:01:16 AM

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 Smile

Sandy
Sandy India
3/23/2009 2:16:54 AM

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....

Radenko Zec
Radenko Zec Bosnia and Herzegovina
4/7/2009 1:54:01 AM

I am glad I can help Smile

Pingbacks and trackbacks (2)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading