M
mahajan.sanjeev
Hi,
I am having problems with rollback using the SQLTransaction object. I
am trying to insert records in two tables in a transaction. I want to
rollback all the changes if any exception occurs in any of the inserts.
But the SQLTransaction object only rolls back the inserts that happen
before an exception. All inserts after the exception go through. What
am I doing wrong? Is it that I cannot do any more Inserts using the
transaction object once the exception is thrown? I need to rollback at
the first exception?
Here is my code snippet
==================================================
Dim cnTracker As New
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Item("sqlconntracker"))
Dim cmdTracker As New SqlCommand
Dim transTracker As SqlTransaction
Dim sSQL As String
Dim bError As Boolean
cnTracker.Open()
cmdTracker.Connection = cnTracker
transTracker = cnTracker.BeginTransaction(IsolationLevel.ReadCommitted)
cmdTracker.Transaction = transTracker
bError = False
Try
'This Statement get rolled back
sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
[nClientFieldID], [bDefaultValue]) VALUES ('Tiffany Havlicek', 215,
4069, NULL)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
Catch ex As Exception
bError = True
End Try
Try
'This statement causes an exception
sSQL = "insert into tblPortfolio (nClientID , Allocation_Date_74_6,
Land_Manager_81_11) VALUES (215, convert(datetime, 'assas') , 2996)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
Catch ex As Exception
bError = True
End Try
Try
'This Statement does NOT get rolled back!!!!
sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
[nClientFieldID], [bDefaultValue]) VALUES ('Riaan', 215, 4069, NULL)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
Catch ex As Exception
bError = True
End Try
If (bError) Then
transTracker.Rollback()
Else
transTracker.Commit()
End If
I am having problems with rollback using the SQLTransaction object. I
am trying to insert records in two tables in a transaction. I want to
rollback all the changes if any exception occurs in any of the inserts.
But the SQLTransaction object only rolls back the inserts that happen
before an exception. All inserts after the exception go through. What
am I doing wrong? Is it that I cannot do any more Inserts using the
transaction object once the exception is thrown? I need to rollback at
the first exception?
Here is my code snippet
==================================================
Dim cnTracker As New
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Item("sqlconntracker"))
Dim cmdTracker As New SqlCommand
Dim transTracker As SqlTransaction
Dim sSQL As String
Dim bError As Boolean
cnTracker.Open()
cmdTracker.Connection = cnTracker
transTracker = cnTracker.BeginTransaction(IsolationLevel.ReadCommitted)
cmdTracker.Transaction = transTracker
bError = False
Try
'This Statement get rolled back
sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
[nClientFieldID], [bDefaultValue]) VALUES ('Tiffany Havlicek', 215,
4069, NULL)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
Catch ex As Exception
bError = True
End Try
Try
'This statement causes an exception
sSQL = "insert into tblPortfolio (nClientID , Allocation_Date_74_6,
Land_Manager_81_11) VALUES (215, convert(datetime, 'assas') , 2996)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
Catch ex As Exception
bError = True
End Try
Try
'This Statement does NOT get rolled back!!!!
sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
[nClientFieldID], [bDefaultValue]) VALUES ('Riaan', 215, 4069, NULL)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
Catch ex As Exception
bError = True
End Try
If (bError) Then
transTracker.Rollback()
Else
transTracker.Commit()
End If