Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Begin/End Transaction VB.NET

Posted on 2004-03-24
7
Medium Priority
?
2,574 Views
Last Modified: 2010-05-19
I have a VB.Net application that calls a couple of stored procedures that update a couple of tables before updating the final table.  I have a trigger which checks a couple of conditions when updating the final table.  If they don't meet, it rolls back the transaction.  I want to be able to roll back all transactions.  Can I:

Have a begin transaction and end transaction within my .net application which would surround the stored procedure calls and the update on the main table (even though the main table has a begin and end transaction of its own in the trigger).  I want to be able to rollback all changes.

Thanks

0
Comment
Question by:barnetjeb
[X]
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
  • 4
  • 3
7 Comments
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 2000 total points
ID: 10670575
You certainly can.

        Dim conn As New SqlConnection("ConnectionString")
        conn.Open()

        Dim com As SqlCommand = conn.CreateCommand()
        Dim sTrans As SqlTransaction

        sTrans = conn.BeginTransaction()
        com.Transaction = sTrans

        'set command text, execute procedures, etc...

        sTrans.Commit()
or
        sTrans.Rollback()
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 10670583
just make sure you're using the same COMMAND object to execute all of the procedures or sql calls
0
 

Author Comment

by:barnetjeb
ID: 10681247
How do I remove the parameters after each call?
0
Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 
LVL 23

Expert Comment

by:Snarf0001
ID: 10682876
com.Parameters.Clear()
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 10682946
Technically I guess there is no reason to actually only use one command object (I've just always done so because it seems cleaner to me, why make multiple command objects unless you have to?).

If you create multiple command objects, as long as you create them off the same connection, and assign them the same SqlTransaction object, it should work fine, I've just never had an occasion yet where one object wouldn't suffice.

The command above will clear out all of the parameters if you wish to do it that way though, and then just reassign .CommandText (and .CommandType if needed).
0
 

Author Comment

by:barnetjeb
ID: 10687564
I'm getting this error:
Execute requires the command to have a transaction object when the connection assigned to the command is in a pending local transaction.  The transaction property of the command has not been initialized.

This command will call 3 stored procedures.  The last stored proc will do an update on a table, which has a trigger.  It checks a couple of conditions and then is rollsback the transaction if they are not met (this transaction exists only in the trigger).  The transaction I want to make will surround all 3 calls.

Can I remove this transaction and have my trigger return something?  If so, how do I do this?

Here is my code:
con is a global to this class and is initialized in the page load event.

Dim cmd as SqlCommand = new sqlCommand()
Dim sTrans as SqlTransaction
cmd.Connection = con
con.Open
cmd.Connection.BeginTransaction()
cmd.Transaction = sTrans
'Do some stuff
'If good - sTrans.Commit()
'Else - sTrans.Rollback()
con.Close
0
 

Author Comment

by:barnetjeb
ID: 10687581
Sorry that was confusing....

begin trans 1
stored proc 1
stored proc 2
stored proc 3
 ....fires trigger - begin trans 2, end trans 2
end trans 1

I would like to get rid of trans 2 and just return something from my trigger that says the conditions were not met.  Then only be using 1 transaction and rollback everything when there is a problem in one of the three stored procs.

Thanks for all your help!!
0

Featured Post

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

661 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