Thread: TypeInitializationException Confusion

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    Angry TypeInitializationException Confusion

    I have a question because the results are driving me insane....

    I have a .NET C# application VS 2005 .NET 2.0, I wrote a very simple console application, that from the main method or function it calls a worker threads and inside the worker threads each thread declares a sql connection and opens and runs sql transactions.

    The code on one computer compiles but when runs and jumps into the worker thread throws a TypeInitializationException error when initializing the sql connection:

    Code:
    SqlConnection conn = new SQLConnection(connstring);
    6 hours of research and I don't know how many different attempts to alter the code, I finally take the original code the problem started with and copy it to another computer with same compiler and versions of .NET and it works fine.....

    Two versions in case anyone can help me understand this....

    error code:
    Code:
     static void Main(string[] args)
            {
    //start batch monitor thread
                Thread t = new Thread(new ThreadStart(monitor_BatchPrint));
                t.IsBackground = true;
                t.Start();
    
                Console.Write("have already called worker thread");
                System.Threading.Thread.Sleep(30000);
    
            }
    
            private static void monitor_BatchPrint ()
            {
                //open db connection
                string connstring;
                connstring = "server=.\\SQLEXPRESS" + ";integrated security=SSPI;" + "database=FXGDB";
                SqlConnection conn = new SqlConnection(conn);
                Console.Write("/n !!!!!!inside first call we are doing good here");
    }
    working code
    Code:
    static void Main(string[] args)
            {
                Console.Write("Hello processing thread");
                Thread worker = new Thread(new ThreadStart(sql_thread));
                worker.IsBackground = true;
                worker.Start();
                System.Threading.Thread.Sleep(20000);
                worker.Abort();
    
            }
            public static void sql_thread()
            {
            /*    //declare connection string storage variable for sql connection
                string connstring;
                //create connection string with server and db information
                connstring = "server=.\\SQLEXPRESS" + ";integrated security=SSPI;" + "database=FXGDB";
                SqlConnection conn = new SqlConnection(connstring);
                conn.Open();
                Console.Write("db open");
                System.Threading.Thread.Sleep(5000);
                conn.Close(); */
                //open db connection
                string connstring;
                connstring = "server=.\\SQLEXPRESS" + ";integrated security=SSPI;" + "database=FXGDB";
                SqlConnection connection = new SqlConnection(connstring);
                Console.Write("/n !!!!!!inside first call we are doing good here");
                Process pBatchPrt = null;
    
            }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Moved to C#
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    type initialization exceptions occur when there's an error initializing a class BEFORE execution of the Main() method. as far as i can tell the 2 versions are pretty much the same. your problem is probably elsewhere. check for any static instances you have defined.

    or u can do it the quick and lazy way and go to debug -> exceptions -> and turn on break into debugger for all uncaught exceptions. run the program and it'll break at the line that causes the exception.

    btw, don't use thread.Abort(). it's evil. use a bool flag instead.
    Last edited by bling; 09-20-2008 at 11:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. for loop confusion
    By Enges in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2006, 08:21 AM
  4. Server-net newbie confusion
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2005, 02:08 AM
  5. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM