How to roll back transactions in Try Catch loops?

J

JenHu

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
*---------------------------------*
 
M

Marina

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 said:
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
*---------------------------------*
 
W

William F. Robertson, Jr.

When are you assigning your variable trans?

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

bill
JenHu said:
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
*---------------------------------*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top