D
Dano
Hi all,
I am always reluctant to call something a bug in the very fine .Net
framework seeing as how I am relatively new to it. However I have come
across some behaviour dealing with transactions that I don't think by any
stretch of language can NOT be called a bug. If it is my own lack of
experience that is causing this whole situation, then I apologize.
So here is the situation. I am doing some database deletion of records, and
I use the Transaction object. I then purposefully cause an error to check
that the Transaction roll back is working. Strangely enough I get a "The
Rollback Transaction request has no corresponding Begin Transaction" error.
So it sounds like a scoping problem with Try blocks and the Begin
Transaction right? Nope! After spending several hours slamming my head
against a brick wall, I begin commenting out lines one at a time. After much
experimenting I find it has nothing whatsoever to do with Transactions, or
Begin Transactions. Here is the Code, reduced to as few lines as I could
while still generating the error:
<Code>
Public Sub deleteAuditGroup(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDelete.Click
Dim objConnection As SqlConnection
Dim objTransaction As SqlTransaction
objConnection = New SqlConnection(Application("strConnection"))
Dim objCommand As New SqlCommand()
Try
objConnection.Open()
objTransaction = objConnection.BeginTransaction()
objCommand.Connection = objConnection
objCommand.Transaction = objTransaction
objCommand.CommandType = CommandType.StoredProcedure
objCommand.CommandText = "sp_AuditGroupDriverEntireDelete"
Dim objParam As SqlParameter
objParam = New SqlParameter()
objParam = objCommand.Parameters.Add("@AuditGroupID", SqlDbType.Int)
objParam.Direction = ParameterDirection.Input
objParam.Value = 27 ' Actual int value is irrelevant
' Notice that this is deliberately commented out
'objParam = objCommand.Parameters.Add("@EmployeeID", SqlDbType.Int)
' objParam.Direction = ParameterDirection.Input
' objParam.Value = 5
' This is a bogus parameter not required by the stored procedure to cause
an exception and force a rollback.
objParam = objCommand.Parameters.Add("@blech", SqlDbType.Int)
objParam.Direction = ParameterDirection.Input
objParam.Value = dropAuditGroup.SelectedItem.Value
objCommand.ExecuteNonQuery()
Catch objException As Exception
objTransaction.Rollback()
lblOutput.Text += "**** Error while Deleting Driver From AuditGroup " +
objException.Message() + " <br/>"
objConnection.Close()
Exit Sub
End Try
</Code>
For what it is worth, here is the stored procedure, though I doubt it has
anything to do with this
<Code>
CREATE PROCEDURE sp_AuditGroupDriverEntireDelete
@AuditGroupID int,
@EmployeeID int
AS
DELETE FROM tblAuditGroupDriver
WHERE AuditGroupID=@AuditGroupID
AND EmployeeID=@EmployeeID
GO
</Code>
Now as it stands, the above works fine. It prints out the error stating that
the Blah parameter is not required for the stored procedure, and more
importantly rolls back the transaction. Now all you have to do to call the
pseudo BeginTransaction Error is to uncomment out the EmployeeID parameter
in the above code. Voila! Note, that it has nothing to do with the fact that
EmployeeID is actually required by the stored procedure. ANY additional
bogus parameter will also cause the same behaviour. For some reason in this
case, 2 parameters cause the transaction to be properly rollbacked , but 3
make the server think that there is no BeginTransaction.
I tried this 5 times with two parameters and got the proper message 5 times.
With 3 parameters I got the missing BeginTransaction error all 5 times.
I know for sure something very strange is going on here. Is this truly a
bug, or is it my own ineptitude?
Dano
I am always reluctant to call something a bug in the very fine .Net
framework seeing as how I am relatively new to it. However I have come
across some behaviour dealing with transactions that I don't think by any
stretch of language can NOT be called a bug. If it is my own lack of
experience that is causing this whole situation, then I apologize.
So here is the situation. I am doing some database deletion of records, and
I use the Transaction object. I then purposefully cause an error to check
that the Transaction roll back is working. Strangely enough I get a "The
Rollback Transaction request has no corresponding Begin Transaction" error.
So it sounds like a scoping problem with Try blocks and the Begin
Transaction right? Nope! After spending several hours slamming my head
against a brick wall, I begin commenting out lines one at a time. After much
experimenting I find it has nothing whatsoever to do with Transactions, or
Begin Transactions. Here is the Code, reduced to as few lines as I could
while still generating the error:
<Code>
Public Sub deleteAuditGroup(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDelete.Click
Dim objConnection As SqlConnection
Dim objTransaction As SqlTransaction
objConnection = New SqlConnection(Application("strConnection"))
Dim objCommand As New SqlCommand()
Try
objConnection.Open()
objTransaction = objConnection.BeginTransaction()
objCommand.Connection = objConnection
objCommand.Transaction = objTransaction
objCommand.CommandType = CommandType.StoredProcedure
objCommand.CommandText = "sp_AuditGroupDriverEntireDelete"
Dim objParam As SqlParameter
objParam = New SqlParameter()
objParam = objCommand.Parameters.Add("@AuditGroupID", SqlDbType.Int)
objParam.Direction = ParameterDirection.Input
objParam.Value = 27 ' Actual int value is irrelevant
' Notice that this is deliberately commented out
'objParam = objCommand.Parameters.Add("@EmployeeID", SqlDbType.Int)
' objParam.Direction = ParameterDirection.Input
' objParam.Value = 5
' This is a bogus parameter not required by the stored procedure to cause
an exception and force a rollback.
objParam = objCommand.Parameters.Add("@blech", SqlDbType.Int)
objParam.Direction = ParameterDirection.Input
objParam.Value = dropAuditGroup.SelectedItem.Value
objCommand.ExecuteNonQuery()
Catch objException As Exception
objTransaction.Rollback()
lblOutput.Text += "**** Error while Deleting Driver From AuditGroup " +
objException.Message() + " <br/>"
objConnection.Close()
Exit Sub
End Try
</Code>
For what it is worth, here is the stored procedure, though I doubt it has
anything to do with this
<Code>
CREATE PROCEDURE sp_AuditGroupDriverEntireDelete
@AuditGroupID int,
@EmployeeID int
AS
DELETE FROM tblAuditGroupDriver
WHERE AuditGroupID=@AuditGroupID
AND EmployeeID=@EmployeeID
GO
</Code>
Now as it stands, the above works fine. It prints out the error stating that
the Blah parameter is not required for the stored procedure, and more
importantly rolls back the transaction. Now all you have to do to call the
pseudo BeginTransaction Error is to uncomment out the EmployeeID parameter
in the above code. Voila! Note, that it has nothing to do with the fact that
EmployeeID is actually required by the stored procedure. ANY additional
bogus parameter will also cause the same behaviour. For some reason in this
case, 2 parameters cause the transaction to be properly rollbacked , but 3
make the server think that there is no BeginTransaction.
I tried this 5 times with two parameters and got the proper message 5 times.
With 3 parameters I got the missing BeginTransaction error all 5 times.
I know for sure something very strange is going on here. Is this truly a
bug, or is it my own ineptitude?
Dano