Hello,
I'm trying to do a batch update. I want to insert the form values into a database table. One the form elements is a multi-select box with states that the rest of the fields apply to, so I take the value of the states and put them into an array, and for each state in the array, I insert a new record into the database table with the rest of the field values set by the form element values.
When there is an error I want to be able to rollback the transaction so that no update occured at all, that way, if 5 states worth of values were updated, but on the 6th there was a problem, I want to cancel everything so no update was performed. I cannot seem to get the code to let me do this. Does anyone know what is wrong with my code? (I'm calling a function to perform the action, and passing in the entire Request.Form as the parameter.)
-Ovatvvon :-Q
I'm trying to do a batch update. I want to insert the form values into a database table. One the form elements is a multi-select box with states that the rest of the fields apply to, so I take the value of the states and put them into an array, and for each state in the array, I insert a new record into the database table with the rest of the field values set by the form element values.
When there is an error I want to be able to rollback the transaction so that no update occured at all, that way, if 5 states worth of values were updated, but on the 6th there was a problem, I want to cancel everything so no update was performed. I cannot seem to get the code to let me do this. Does anyone know what is wrong with my code? (I'm calling a function to perform the action, and passing in the entire Request.Form as the parameter.)
Code:
Function updateClassCode(frm)
Dim rstCC, intCounter, arrStates, blnTrucking, blnStateSpecific
arrStates = Split(frm("states_vRequired"), ", ")
If frm("chkTrucking")="on" Then blnTrucking=True Else blnTrucking=False
If frm("chkState")="on" Then blnStateSpecific=True Else blnStateSpecific=False
Set cnnDB = Server.CreateObject("ADODB.Connection")
cnnDB.Open CNN_DB
Set rstCC = Server.CreateObject("ADODB.Recordset")
rstCC.Open "CC_CLASS_CODE", cnnDB, adOpenKeyset, adLockBatchOptimistic, adCmdTableDirect
cnnDB.BeginTrans
On Error Resume Next
For intCounter = 0 to UBound(arrStates)
rstCC.AddNew
rstCC("state") = arrStates(intCounter)
rstCC("class_code") = frm("ClassCode_vRequired")
rstCC("description") = frm("txtDescription")
rstCC("trucking") = blnTrucking
rstCC("state_specific") = blnStateSpecific
rstCC("status") = frm("cboStatus")
rstCC("comment") = frm("txtComments")
Next
rstCC.UpdateBatch
If (err.number <> 0) Or (cnnDB.Errors.Count <> 0) Then
cnnDB.RollbackTrans
addNewClasscode = True
Else
cnnDB.CommitTrans
addNewClassCode = False
End If
rstCC.Close
Set rstCC = Nothing
cnnDB.Close
Set cnnDB = Nothing
End Function
-Ovatvvon :-Q