How to roll back transactions in Try Catch loops?

  • Thread starter Thread starter JenHu
  • Start date Start date
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
*---------------------------------*
 
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
*---------------------------------*
 
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
*---------------------------------*
 
Back
Top