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;
}