Previous | Index | Next 

[PRB] VB.NET doesn’t allow to close an ADO Connection while a transaction is active

In VB6 you can close an ADO Connection while a transaction is active:

        Dim cn As New ADODB.Connection
        cn.Open myConnectionString
        cn.BeginTrans
        ...
        cn.Close

In VB.NET this operation causes a COMException runtime error whose message is quite self-explanatory:

        Connection object cannot be explicitly closed while in a transaction.

You can see this error both while working directly with an ADO Connection and while opening a transaction on the Connection object exposed by an ADO Data control.

In general, closing a connection while a transaction is active is bad programming practice. If you experience this error in the converted VB.NET application we recommend that you edit the original VB6 code to explicitly rollback or commit the transaction before closing the connection. If you prefer not to edit the original VB6 code, just use an InsertStatement pragma, as in:

        Dim cn As New ADODB.Connection
        cn.Open myConnectionString
        cn.BeginTrans
        ...
        '## InsertStatement cn.RollbackTrans
        cn.Close

 

Previous | Index | Next