I'm not sure if this is the right thread but here goes. If I call on a SPROC from VB like so:
Do I need to have a Try-Catch block (for rollbacks) in my SPROC? I'm trying the code out, and deliberately making it fail, but it seems like the code still pulls through and doesn't rollback changes (evidenced by the increase in an Identity field).Code:dbCmd = New SqlCommand("spNewPatient", dbConn, dbTran) dbCmd.CommandType = CommandType.StoredProcedure For Each p As SqlParameter In _params dbCmd.Parameters.Add(p) Next objPatientID = dbCmd.ExecuteScalar() If objPatientID IsNot Nothing Then intPatientID = CInt(objPatientID) End If dbTran.Commit() Catch ex As Exception MsgBox(ex.Message & strHelpDirections, MsgBoxStyle.Exclamation, errParsing) Try If Not dbTran.Connection Is Nothing Then dbTran.Rollback("trnAddProfile") dbTran.Dispose() End If Catch ex2 As SqlException MsgBox("Failed to rollback changes. Please contact your system administrator." & vbCrLf & vbCrLf & "Exception Type: " & ex2.GetType().ToString(), MsgBoxStyle.Exclamation, "Rollback Failed") End Try
In addition, I'm getting a foreign key error if I use the code below instead of using the SQL code above:Code:BEGIN TRY BEGIN TRANSACTION AddPatient INSERT INTO tblPatient ( ... ); SELECT SCOPE_IDENTITY() AS PatientID; COMMIT TRANSACTION END TRY BEGIN CATCH ROLLBACK TRANSACTION AddPatient; END CATCH END
Which is how I can see that there was an increase in the Identity field, however, the new entry was not created which leads me to believe that the transaction did rollback properly.Code:... --SELECT SCOPE_IDENTITY() AS PatientID; COMMIT TRANSACTION RETURN SCOPE_IDENTITY();


Reply With Quote
