G
Guest
Hi Everyone,
I'm currently working on a form that requires 3 stored procs to run (one of
them several times), but these stored procs are in different functions (see
below). The problem is that after the commit, the database doesn't reflect
any of the changes. I open a new connection before calling a proc, and then
pass the same connection to each function and within the function, I set the
transaction to the Command object. Here is the code shortened:
Private Sub cmdSave_Click( ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click
Dim cnSQL as SqlConnection
Dim Success as Boolean = True
try
cnSQL = OpenConnection()
Success = SaveAdmissionNursingAssessment1(cnSQL)
if Success = True then
Success = SaveFamilyHistory(cnSQL)
if not Success then
SaveTrans.Dispose
else
Success = SaveAdmissionNursingAssessment2(cnSQL)
if not Success then
SaveTrans.Dispose
End If
End If
else
cnSQL.Dispose
end if
cmdAdd.Enabled = True
cmdSave.Enabled = False
cmdCancel.Enabled = False
Catch ex As Exception
msgbox(ex.Message )
End Try
End Sub
Private Function SaveAdmissionNursingAssessment1(cnSQL As SqlConnection) as
Boolean
Dim success as Boolean = true
Dim cmSQL As SqlCommand
Try
SaveTrans = cnSQL.BeginTransaction
if isnew then
cmSQL = New SqlCommand("intake_AddAdminNurseAssess1", cnSQL)
cmsql.Transaction = SaveTrans
else
cmSQL = New SqlCommand("intake_UpdAdmissionNursingAssessment1", cnSQL)
cmsql.Parameters.Add("KeyID", sqldbtype.Int ).Value = fAssessId
cmsql.Transaction = SaveTrans
end if
cmsql.CommandType = CommandType.StoredProcedure
cmsql.Parameters.Add("PatientID", sqldbtype.Varchar, 20 ).Value =
fPatientId
cmsql.Parameters.Add("PreparedDate", sqldbtype.SmallDateTime ).Value =
Now()
cmSQL.ExecuteNonQuery()
Catch Exp As SqlException
MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
SaveTrans.Rollback
success = False
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
SaveTrans.Rollback
success = False
finally
' Close and Clean up objects
cmSQL.Dispose()
SaveAdmissionNursingAssessment1 = success
End Try
End Function
Private Function SaveAdmissionNursingAssessment2(cnSQL As SqlConnection) as
Boolean
Dim success as Boolean = True
Dim cmSQL As SqlCommand
Try
if isnew then
cmSQL = New SqlCommand("intake_AddAdminNurseAssessment2", cnSQL)
cmsql.Transaction = SaveTrans
isnew = False
else
cmSQL = New SqlCommand("intake_UpdAdmissionNursingAssessment2", cnSQL)
cmsql.Parameters.Add("KeyID", sqldbtype.Int ).Value = fAssessId
cmsql.Transaction = SaveTrans
isDirty = False
end if
cmsql.CommandType = CommandType.StoredProcedure
cmsql.Parameters.Add("PatientID", sqldbtype.Varchar, 20 ).Value =
fPatientId
cmSQL.ExecuteNonQuery()
SaveTrans.Commit
Catch Exp As SqlException
MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
SaveTrans.Rollback
success = False
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
SaveTrans.Rollback
success = False
finally
' Close and Clean up objects
cnSQL.Close()
cmSQL.Dispose()
cnSQL.Dispose()
SaveTrans.Dispose
SaveAdmissionNursingAssessment2 = success
End Try
End Function
Private Function SaveFamilyHistory(cnSQL As SqlConnection) as Boolean
'Dim cnSQL As SqlConnection
Dim success as Boolean = true
Dim cmSQL As SqlCommand
Dim i as Integer
Try
For i = 2 To flxFamilyHistory1.Rows.Count - 1
if len(trim(flxFamilyHistory1(i, 1))) > 0 or
len(trim(flxFamilyHistory1(i, 3))) > 0 then
if not isnumeric(flxFamilyHistory1(i, 6)) then
cmSQL = New SqlCommand("intake_AddANFamilyHistory", cnSQL)
cmsql.Transaction = SaveTrans
else
cmSQL = New SqlCommand("intake_UpdANFamilyHistory", cnSQL)
cmsql.Parameters.Add("@HistId", sqldbtype.int).Value =
flxFamilyHistory1(i, 0)
cmsql.Transaction = SaveTrans
end if
cmsql.CommandType = CommandType.StoredProcedure
cmsql.Parameters.Add("@AssessId", sqldbtype.Int ).Value = fAssessId
cmsql.Parameters.Add("@PatientID", sqldbtype.Varchar, 20 ).Value =
fPatientId
cmsql.Parameters.Add("@Age", sqldbtype.Varchar, 5 ).Value =
flxFamilyHistory1(i, 1)
cmsql.Parameters.Add("@Health", sqldbtype.Varchar, 25 ).Value =
flxFamilyHistory1(i, 2)
cmsql.Parameters.Add("@AgeOfDeath", sqldbtype.Varchar, 50 ).Value =
flxFamilyHistory1(i, 3)
cmsql.Parameters.Add("@CauseOfDeath", sqldbtype.Varchar, 50 ).Value
= flxFamilyHistory1(i, 4)
cmsql.Parameters.Add("@Who", sqldbtype.Varchar, 50 ).Value =
flxFamilyHistory1(i, 5)
cmsql.Parameters.Add("@SortOrder", sqldbtype.Varchar, 50 ).Value = i
cmSQL.ExecuteNonQuery()
End If
Next
Catch Exp As SqlException
MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
SaveTrans.Rollback
success = False
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
SaveTrans.Rollback
success = False
finally
cmSQL.Dispose()
SaveFamilyHistory = success
End Try
end function
Thanks for any info. Is there a better way to handle this when crossing
function with our code. Thanks so much for any info.
Michael
I'm currently working on a form that requires 3 stored procs to run (one of
them several times), but these stored procs are in different functions (see
below). The problem is that after the commit, the database doesn't reflect
any of the changes. I open a new connection before calling a proc, and then
pass the same connection to each function and within the function, I set the
transaction to the Command object. Here is the code shortened:
Private Sub cmdSave_Click( ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click
Dim cnSQL as SqlConnection
Dim Success as Boolean = True
try
cnSQL = OpenConnection()
Success = SaveAdmissionNursingAssessment1(cnSQL)
if Success = True then
Success = SaveFamilyHistory(cnSQL)
if not Success then
SaveTrans.Dispose
else
Success = SaveAdmissionNursingAssessment2(cnSQL)
if not Success then
SaveTrans.Dispose
End If
End If
else
cnSQL.Dispose
end if
cmdAdd.Enabled = True
cmdSave.Enabled = False
cmdCancel.Enabled = False
Catch ex As Exception
msgbox(ex.Message )
End Try
End Sub
Private Function SaveAdmissionNursingAssessment1(cnSQL As SqlConnection) as
Boolean
Dim success as Boolean = true
Dim cmSQL As SqlCommand
Try
SaveTrans = cnSQL.BeginTransaction
if isnew then
cmSQL = New SqlCommand("intake_AddAdminNurseAssess1", cnSQL)
cmsql.Transaction = SaveTrans
else
cmSQL = New SqlCommand("intake_UpdAdmissionNursingAssessment1", cnSQL)
cmsql.Parameters.Add("KeyID", sqldbtype.Int ).Value = fAssessId
cmsql.Transaction = SaveTrans
end if
cmsql.CommandType = CommandType.StoredProcedure
cmsql.Parameters.Add("PatientID", sqldbtype.Varchar, 20 ).Value =
fPatientId
cmsql.Parameters.Add("PreparedDate", sqldbtype.SmallDateTime ).Value =
Now()
cmSQL.ExecuteNonQuery()
Catch Exp As SqlException
MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
SaveTrans.Rollback
success = False
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
SaveTrans.Rollback
success = False
finally
' Close and Clean up objects
cmSQL.Dispose()
SaveAdmissionNursingAssessment1 = success
End Try
End Function
Private Function SaveAdmissionNursingAssessment2(cnSQL As SqlConnection) as
Boolean
Dim success as Boolean = True
Dim cmSQL As SqlCommand
Try
if isnew then
cmSQL = New SqlCommand("intake_AddAdminNurseAssessment2", cnSQL)
cmsql.Transaction = SaveTrans
isnew = False
else
cmSQL = New SqlCommand("intake_UpdAdmissionNursingAssessment2", cnSQL)
cmsql.Parameters.Add("KeyID", sqldbtype.Int ).Value = fAssessId
cmsql.Transaction = SaveTrans
isDirty = False
end if
cmsql.CommandType = CommandType.StoredProcedure
cmsql.Parameters.Add("PatientID", sqldbtype.Varchar, 20 ).Value =
fPatientId
cmSQL.ExecuteNonQuery()
SaveTrans.Commit
Catch Exp As SqlException
MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
SaveTrans.Rollback
success = False
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
SaveTrans.Rollback
success = False
finally
' Close and Clean up objects
cnSQL.Close()
cmSQL.Dispose()
cnSQL.Dispose()
SaveTrans.Dispose
SaveAdmissionNursingAssessment2 = success
End Try
End Function
Private Function SaveFamilyHistory(cnSQL As SqlConnection) as Boolean
'Dim cnSQL As SqlConnection
Dim success as Boolean = true
Dim cmSQL As SqlCommand
Dim i as Integer
Try
For i = 2 To flxFamilyHistory1.Rows.Count - 1
if len(trim(flxFamilyHistory1(i, 1))) > 0 or
len(trim(flxFamilyHistory1(i, 3))) > 0 then
if not isnumeric(flxFamilyHistory1(i, 6)) then
cmSQL = New SqlCommand("intake_AddANFamilyHistory", cnSQL)
cmsql.Transaction = SaveTrans
else
cmSQL = New SqlCommand("intake_UpdANFamilyHistory", cnSQL)
cmsql.Parameters.Add("@HistId", sqldbtype.int).Value =
flxFamilyHistory1(i, 0)
cmsql.Transaction = SaveTrans
end if
cmsql.CommandType = CommandType.StoredProcedure
cmsql.Parameters.Add("@AssessId", sqldbtype.Int ).Value = fAssessId
cmsql.Parameters.Add("@PatientID", sqldbtype.Varchar, 20 ).Value =
fPatientId
cmsql.Parameters.Add("@Age", sqldbtype.Varchar, 5 ).Value =
flxFamilyHistory1(i, 1)
cmsql.Parameters.Add("@Health", sqldbtype.Varchar, 25 ).Value =
flxFamilyHistory1(i, 2)
cmsql.Parameters.Add("@AgeOfDeath", sqldbtype.Varchar, 50 ).Value =
flxFamilyHistory1(i, 3)
cmsql.Parameters.Add("@CauseOfDeath", sqldbtype.Varchar, 50 ).Value
= flxFamilyHistory1(i, 4)
cmsql.Parameters.Add("@Who", sqldbtype.Varchar, 50 ).Value =
flxFamilyHistory1(i, 5)
cmsql.Parameters.Add("@SortOrder", sqldbtype.Varchar, 50 ).Value = i
cmSQL.ExecuteNonQuery()
End If
Next
Catch Exp As SqlException
MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
SaveTrans.Rollback
success = False
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
SaveTrans.Rollback
success = False
finally
cmSQL.Dispose()
SaveFamilyHistory = success
End Try
end function
Thanks for any info. Is there a better way to handle this when crossing
function with our code. Thanks so much for any info.
Michael