This is a .NET 2.0 application written using VS 2005. It works fine on systems running .NET 2.0, but hard crashes on systems running .NET 4.0. Here's the critical section of the code:
string selectCommand1 = ....
string connectionString1 = ....
using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand1, connectionString1))
{
try
{
adapter.Fill(table1);
}
catch
{
MessageBox.Show("error");
}
}
string selectCommand2 = ....
string connectionString2 = ....
using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand2, connectionString2))
{
try
{
adapter.Fill(table2);
}
catch
{
MessageBox.Show("error");
}
}
Again, it works under .NET 2.0, crashes under .NET 4.0
ConnectionStrings 1 & 2 reference different .xls files.
I found that the way around this problem is to declare and initialize a field variable of type OleDbConnection, set the ConnectionString property and Open() it before the OleDbDataAdapter's using statement. As so:
OleDbConnection connection = new OleDbConnection();
.......
connection.ConnectionString = connectionString1;
connection.Open();
using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand1, connection))
{
try
{
adapter.Fill(table1);
}
catch
{
MessageBox.Show("error");
}
}
connection.Close();
connection.ConnectionString = connectionString2;
connection.Open();
using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand2, connection))
{
try
{
adapter.Fill(table2);
}
catch
{
MessageBox.Show("error");
}
}
It's hard to believe that this is the reason why my app was hard crashing (no error messages) under .NET 4.0, but after removing lines of code one at a time and recompiling over and over I found that to be the cause of the problem.
I'm glad I solved the problem, but I'm not satisfied with the fact that the first code won't work with .NET 4.0.
Can someone please explain why .NET 4.0 doesn't like to work with code like the one above?
The problem was "Application Verifier". Uninstalling it solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With