473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to roll back transactions in Try Catch loops?

Hi experts,

I want to rollback the insert/update in my SQL Server database
tables if there's any errors, and I use trans.Rollback(). However,
whenever error generates, the rollback doesn't work at all, and it
throws this server error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

can anyone tell me what does this go wrong? Thanks.
------------------------------------------------------

Dim trans As System.Data.SqlClient.SqlTransaction
Try
.........some codes...........

Catch exp As Exception

'rollback the transaction if any step above is not successful
trans.Rollback()

End Try
*---------------------------------*
Posted at: http://www.GroupSrv.com
Check: http://wwww.HotCodecs.com
*---------------------------------*

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 19 '05 #1
2 3522
You cut out most of the code - what could we tell you?

You have to make sure the transaction was actually set when you try to roll
it back. If you have other code that might cause an exception before the
transaction is started - then there is no transaction to roll back.

"JenHu" <je**********@hotmail-dot-com.no-spam.invalid> wrote in message
news:41********@127.0.0.1...
Hi experts,

I want to rollback the insert/update in my SQL Server database
tables if there's any errors, and I use trans.Rollback(). However,
whenever error generates, the rollback doesn't work at all, and it
throws this server error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

can anyone tell me what does this go wrong? Thanks.
------------------------------------------------------

Dim trans As System.Data.SqlClient.SqlTransaction
Try
.........some codes...........

Catch exp As Exception

'rollback the transaction if any step above is not successful
trans.Rollback()

End Try
*---------------------------------*
Posted at: http://www.GroupSrv.com
Check: http://wwww.HotCodecs.com
*---------------------------------*

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 19 '05 #2
When are you assigning your variable trans?

try
trans = connection.BeginTransaction()
'run queries
catch
if NOT trans = null then
trans.RollBack()
endif

bill
"JenHu" <je**********@hotmail-dot-com.no-spam.invalid> wrote in message
news:41********@127.0.0.1...
Hi experts,

I want to rollback the insert/update in my SQL Server database
tables if there's any errors, and I use trans.Rollback(). However,
whenever error generates, the rollback doesn't work at all, and it
throws this server error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

can anyone tell me what does this go wrong? Thanks.
------------------------------------------------------

Dim trans As System.Data.SqlClient.SqlTransaction
Try
.........some codes...........

Catch exp As Exception

'rollback the transaction if any step above is not successful
trans.Rollback()

End Try
*---------------------------------*
Posted at: http://www.GroupSrv.com
Check: http://wwww.HotCodecs.com
*---------------------------------*

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andy Hall | last post by:
Hi, I am rolling back a transaction that (up to the rollback command) has inserted into two tables, both of which are type InnoDB. I am getting the warning: "Warning: Some non-transactional...
2
by: Rodrigo GarcĂ­a | last post by:
Hi. The problem is a bit hard to explain. I have a ServicedComponent method which runs a distributed transaction consisting on a a MessageQueue reception and several database operations. That...
11
by: Mike P | last post by:
I've been using C# transactions for a while and had no problems with them. Using try catch blocks I can trap basically all possible errors and rollback all necessary data. Over the last few...
12
by: reycri | last post by:
While the following is allowed: if (a == b) SomeFunction(); else OtherFunction(); The following is not: try
2
by: Diego F. | last post by:
Hello. I'm writing some code to connect and execute queries using oracle namespace in VB.NET. My question is about the use of transactions. To use oracle transactions, I must do something like:...
1
by: elma.arsalan | last post by:
Hello: Does anyone know whether it is possible to roll back MS Access database without loosing any data? Or can a roll back be undone (kinda odd). Any comment would be appreciated.
4
by: clairelee0322 | last post by:
Hello guys again! I am a C++ beginner and I am working on another project.. It's called Dice Lab with Loops.. unforunately I don't even know how to start the program.... Don't blame me for not pay...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
I have to read data from an Excel Sheet. Using Microsoft Office Interop and Automation I create an Excel object Dim xl As New Excel.Application, wkbk As Excel.Workbook, rng as Excel.Range I...
0
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
All I want to use sql transactions on one of my forms as I have multiple updates to tables to perform and want all or none to commit. The problem I've got is that each update to a table is...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.