Update: Sorry for the delay over the weekend but some updates to your comments: I have updated the stack trace to include the entire stack. As well, the problems still exists when I manually close the reader via DataReader.Close(). The database that I am using is just a Microsoft Access databse. Finally, If I catch the OdbcException, this is the information in the exception:
OdbcException:
Data: System.Collections.ListDictionaryInternal
ErrorCode: -2146232009 (Most likely no help)
Errors: System.Data.OdbcErrorCollecion
HelpLink: [blank]
InnerException: [blank]
Message: [blank]
Source: [blank]
TargetSite: Void StatementErrorHandler(RetCode)
End Update.
Is there any reason the OdbcDataReader.Close()
method would function normally in Visual Studio when debugging, but would throw an OdbcException on the Close method when the EXE is run outside of Visual Studio?
A little background: I have an application that performs some basic interaction with a database, and I have been interacting with the database by using a standard format (shown below) held in a Database class. This class has been working for around 6 months when recently it started to crash when the program is run from the EXE only. It would throw an unhandled exception OdbcException
when the DataReader was closed (at the end of a using statement. Same exception if the Close() method is called). The stack trace for the exception is (with some names changed):
at System.Data.Odbc.CMDWrapper.StatementErrorHandler(RetCode retcode)
at System.Data.Odbc.CMDWrapper.FreeStatementHandle(STMT stmt)
at System.Data.Odbc.OdbcDataReader.Close(Boolean disposing)
at System.Data.Odbc.OdbcDataReader.Dispose(Boolean disposing)
at System.Data.Common.DbDataReader.Dispose()
at Namespace.DatabaseManag.Retrieve(String column, String table) in C:\Program\CommonFiles\DatabaseManag.cs:line 81
at Namespace.Parameters.LoadData() in C:\UserControls\Parameters.cs:line 49
at Namespace.MainScreen.OnLoad(Object sender, EventArgs e) in C:\Program\MainScreen.cs:line 99
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
When launched from Visual Studio everything functions normally. It is only when the EXE is ran that this problem occurs. Through a lot of trial and error I was able to remove pieces from the Database class until I could narrow it down between a method that would work and a method that would throw the exception. The only difference between the two methods was an empty catch clause shown below:
Method that does not work:
public List<string> Retrieve(string column, string table)
{
List<string> Results = new List<string>();
try
{
using (OdbcConnection Conn = new OdbcConnection("DSN=DSNNAME"))
{
using (OdbcCommand Command = new OdbcCommand("select " + column + " from " + table, Conn))
{
Conn.Open();
using (OdbcDataReader Reader = Command.ExecuteReader())
{
while (Reader.Read())
{
Results.Add(Reader.GetString(0));
}
} //This is the line that it will fail on
}
}
}
finally
{
}
return Results;
}
Method that does work (By works, I mean that the information from the database is still read and returned correctly, but the exception is ignored):
public List<string> Retrieve(string column, string table)
{
List<string> Results = new List<string>();
try
{
using (OdbcConnection Conn = new OdbcConnection("DSN=DSNNAME"))
{
using (OdbcCommand Command = new OdbcCommand("select " + column + " from " + table, Conn))
{
Conn.Open();
using (OdbcDataReader Reader = Command.ExecuteReader())
{
while (Reader.Read())
{
Results.Add(Reader.GetString(0));
}
}
}
}
}
catch
{
}
finally
{
}
return Results;
}
I cannot find a reason why this would occur. And when trying to replicate this in a separate project it would not throw the exception which leads me to believe is related to my specific project. However, when the blank catch clause is added into the full Database class, it still throws the exception.
Does anyone know why this is occurring? I'm getting the feeling something is corrupted but reinstalling all relevant applications had no effect.