locked
Application fails at OdbdConnection.Open command RRS feed

  • Question

  • I have a 32-bit application using ODBC database connection to MS Access files and 32-bit ODBC drivers. The WinForm application is written in C# using Visual Studio 2017 on a Windows 10 v1909 computer. My problem is that, after a small number of successful accesses to a table, a subsequent access fails at a table Open command. The diagnostic stream is:

    System.Data.Odbc.OdbcException (0x80131937)
       at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
       at System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr,      OdbcEnvironmentHandle environmentHandle)
       at System.Data.Odbc.OdbcConnectionOpen..ctor(OdbcConnection outerConnection, OdbcConnectionString connectionOptions)
       at System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
       at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       at System.Data.Odbc.OdbcConnection.Open()

    If I close and restart the application and then rerun the same operation, no error is generated. This leads me to believe that I haven't properly cleaned up after a previous operation, but the only cleanup operation I think is needed is to close the previously opened table, and I have reviewed the code to assure that I have done this in all instances. I have searched forums and Microsoft documentation for guidance on this problem and have found nothing that quite matches this circumstance. I'm hoping someone has some insight into this problem and can suggest possible solutions.

    Saturday, July 25, 2020 9:40 PM

Answers

  • Try wrapping instantiation of objects that implement IDisposable (OdbcConnection, OdbcCommand, etc.) in using blocks to ensure they are properly (and automatically) cleaned up. This also avoids the need to invoke Close explicitly.

    Dan Guzman, Data Platform MVP, http://www.dbdelta.com

    Saturday, July 25, 2020 9:49 PM
  • Hi Jerry Horsewood, 
    You can also use odbcconnection.open inside a using block.
    Using statement is designed to close automatically resources when the variable is out of scope. 

    using (OdbcConnection connection = new OdbcConnection(connectionString))
    {
            connection.Open();
            // ......
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 28, 2020 3:02 AM

All replies

  • Try wrapping instantiation of objects that implement IDisposable (OdbcConnection, OdbcCommand, etc.) in using blocks to ensure they are properly (and automatically) cleaned up. This also avoids the need to invoke Close explicitly.

    Dan Guzman, Data Platform MVP, http://www.dbdelta.com

    Saturday, July 25, 2020 9:49 PM
  • Hi Jerry Horsewood, 
    You can also use odbcconnection.open inside a using block.
    Using statement is designed to close automatically resources when the variable is out of scope. 

    using (OdbcConnection connection = new OdbcConnection(connectionString))
    {
            connection.Open();
            // ......
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 28, 2020 3:02 AM
  • Thanks for the suggestion. After receiving the suggestion from @Dan_Guzman, I went through the code of the application, wrapping all or the hundreds of connection.open statements inside using blocks in the exact manner shown by @Daniel_Zhang. After several days of testing, the problem has yet to recur and I believe the problem is resolved. Your prompt response is much appreciated.
    Thursday, July 30, 2020 2:00 PM