Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


multiple transactions

Posted on 2012-03-30
Medium Priority
Last Modified: 2012-03-30

I want to use transactions in a vb.net application. This is the working code I have so far:

       Dim sqlCommand As String = "SELECT * FROM tblMyTable"

        Dim da1 As New SqlClient.SqlDataAdapter

        da1 = New SqlClient.SqlDataAdapter(sqlCommand, objConnection)

        Dim cmdClientsBuilder As New SqlClient.SqlCommandBuilder
        cmdClientsBuilder = New SqlClient.SqlCommandBuilder(da1)
        da1.InsertCommand = cmdClientsBuilder.GetInsertCommand()
        da1.DeleteCommand = cmdClientsBuilder.GetDeleteCommand()
        da1.UpdateCommand = cmdClientsBuilder.GetUpdateCommand()

        Dim txn As SqlClient.SqlTransaction = objConnection.BeginTransaction

        da1.UpdateCommand.Transaction = txn
        da1.DeleteCommand.Transaction = txn
        da1.InsertCommand.Transaction = txn

            Console.WriteLine("Insert succeeded")
        Catch ex As Exception
            Console.WriteLine("Insert Failed")
            'Optional. A Finally block is always executed when execution leaves any part of the Try statement.
        End Try

HOWEVER, I am puzzled how to use this if I had another update which I wanted to perform. For example, suppose I wanted to update another table as well e.g. dt2

            Console.WriteLine("Insert succeeded")

To do this, would I need to have two transaction objects?

Could somebody point me in the right direction, with example code, on how to do this?

Thanks in advance

Question by:IssacJones
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

gpizzuto earned 1200 total points
ID: 37786487
You have to set the transaction property also to da2.

da2.UpdateCommand.Transaction = txn
da2.DeleteCommand.Transaction = txn
da2.InsertCommand.Transaction = txn

Beware: the transaction will rollback BOTH the updates if one of them fails.

Author Closing Comment

ID: 37786565
Many thanks!!!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question