By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,660 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,660 IT Pros & Developers. It's quick & easy.

How to roll back transactions in Try Catch loops?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.