Visual Basic Transactions

Posted on 2007-03-29
Last Modified: 2012-08-13
Visual basic transactions - I have stored procedures called from visual basic. How do I create transactions in visual basic?

For example, I'd like to do:

start transaction
vb code
stored procedure call
vb code
stored procedure call
if error, rollback
Question by:mhunts
LVL 18

Expert Comment

by:Ajay Sharma
ID: 18821915

Author Comment

ID: 18821959
Not quite. The example only calls one stored procedure. Look at my question again.
LVL 18

Assisted Solution

DarrenD earned 100 total points
ID: 18822092

I have some code in C# to do this if you want to take it and convert it into VB.NET. I haven't out up any code as yet as I'm not sure if C# is any use to you.

Put simply you have to do the following:

This is performed in one method to setup the connection and transaction

// Create the connection.
// open the connection.
// Set the beginning of the transaction.

This is performed in another method so that you can call as many SP's as you want.
// Tell the command object the name of the SP to run.
// Tell the command object that it is part of a transaction.
// Tell the command object that it is going to run a SP.
// Execute the Query

Each time you call the second method above you must clear any parameters that you added for the call to the previous SP.

Then there is another method which I use to either commit the transaction or rollback.

I just have a simgle class which performs all of these operation for me. It's just a DAL.


Accepted Solution

dctuck earned 400 total points
ID: 18822589
       Dim myTrans As System.Data.SqlClient.SqlTransaction
        Dim myConn As New SqlClient.SqlConnection("connection string")
            myTrans = myConn.BeginTransaction
            Dim myComm As New SqlClient.SqlCommand("Command text",myConn)
            myComm.Transaction = myTrans
           ' Perform your actions....
           ' Repeat the previous two steps for any additional commands
        End Try

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate ( First, just open a new email message. In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now