Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I need help with a piece of code I am writing... I thought this was really genious, but it turns out it doesn't work. Howcome?

 

Here is the code:

 

Try
           m_conOleDb.ConnectionString = g_ConnStrings.GetInstance.GetConnectStringByPhase(Request.DesignPhase)
           m_conOleDb.Open()

           With cmdOleDb
               .Connection = m_conOleDb
               .CommandText = Request.Command
               .CommandType = Request.CommandType
           End With

           'If any parameters exist in the Request object, add them
           If Request.Parameters.Count > 0 Then
               For Each oParam In Request.Parameters
                   prmOleDb = cmdOleDb.Parameters.Add(oParam.ParamName, oParam.ParamValue)
               Next
           End If

           'We check if the command uses a transaction or not
           If Request.Transactional Then
               tranOleDb = m_conOleDb.BeginTransaction
           End If

           'Create a dataadapter and pass the command as a parameter
           daOleDb = New OleDbDataAdapter(cmdOleDb)
           daOleDb.Fill(oDataSetOleDb.ReturnedDataSet)

           Return oDataSetOleDb

       Catch exOleDb As OleDbException
           Request.Exception = exOleDb
           If Request.Transactional Then
               tranOleDb.Rollback()
           End If
       Catch ex As Exception
           Request.Exception = ex
           If Request.Transactional Then
               tranOleDb.Rollback()
           End If
       Finally
           If Request.Transactional Then
               tranOleDb.Commit()
           End If
           If m_conOleDb.State = ConnectionState.Open Then
               m_conOleDb.Close()
           End If
       End Try

 

the problem is wiht the transactions. I get an exception on the line " tranOleDb.Commit()"

 

saying "This OleDbTransaction has completed; it is no longer usable"

 

 

What is the problem? If I don't use transactions, it works just great :)

 

kind regards

Henrik

  • Administrators
Posted

The finally block will get executed if the code in the try block suceeds or fails, if it fails one of the tranOleDB.Rollback() statements will get executed and then it will try to call tranOleDb.Commit() in the finally block.

You may want to move the commit part to just before the line

 

Return oDataSetOleDb in the try block.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

thanks for the reply, but that resulted in an empty dataset :(

 

If I run the code without transactions, it works great

 

 

If you are curious this is a data tier based on singleton and abstract factory patterns... that is why I have wrapper classes for everything, even datasets and datareaders

 

 

yours

Henrik

Posted

Why do you need to use a transaction here? It looks like you're just reading data, and you don't need a transaction for that.

 

A transaction is best when you have to send multiple changes, and you want ALL of them to succeed or all to fail.

 

But you definetly need to move the commit out of the finally statement, even if theres an error it will run.

Posted

well this data tier is using command objects and can execute both SPs textcommands and tablecommands. It is not only for reading data...

 

kind regards

HEnrik

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...