|
Building Our Order Processing
Page (cont'd)
Page 18 of 22 - Chapter
14
If
all has gone without error then we can start processing the order items. We
create the command object we will use to execute the AddOrderItem stored
procedure then we append the parameters.
// check if stored
procedure executed ok
// abort transaction if failed
if (loCommand.Parameters("RV") != 0)
{
// get Stored Procs return VALUE
iErrorNo =
loCommand.Parameters("RV");
ObjectContext.SetAbort;
}
else
{
// Retrieve CustId and OrderId from
// stored procs output variables
iCustId =
loCommand.Parameters("CustId");
iOrderId =
loCommand.Parameters("OrderId");
// Create new command object
// to add each order item detail to
database
loCommand = null;
loCommand =
Server.CreateObject("ADODB.Command");
loCommand.CommandText = "AddOrderItem";
loCommand.CommandType = adCmdStoredProc;
loCommand.Name = "AddOrderItem";
//Append Parameters @OrderId int, @ItemId
int, @Qty int
loParam =
loCommand.CreateParameter("RV", adInteger, _
adParamReturnValue);
loCommand.Parameters.Append(loParam);
loParam =
loCommand.CreateParameter("OrderId", adInteger, _
adParamInput,0,iOrderId);
loCommand.Parameters.Append(loParam);
loParam =
loCommand.CreateParameter("ItemId", adInteger, _
adParamInput);
loCommand.Parameters.Append(loParam);
loParam =
loCommand.CreateParameter("Qty", adInteger, _
adParamInput);
loCommand.Parameters.Append(loParam);
loCommand.ActiveConnection = loConn; |
Having created our Command
object we can reuse it as we loop through the form elements containing the
ItemIds and quantities making up the order. After each item has been added using
the stored procedure we check that the return value is zero indicating no
errors. If there were errors then we call ObjectContext.SetAbort and break out
of the loop. Calling SetAbort will roll back all the order items already added
to the database as well as the customer and order details added to the database.
We are therefore assured that the database will not become corrupted by half
completed transactions.
Remember
that a return value of 547 indicates an error due to insufficient stock and we
will write the code to handle that soon. If it's any other error then the
OnTransactionAbort event, which fires as a result of calling SetAbort, will
redirect the response to transerror.asp – a page we have yet to create.
Assuming
that all has gone well, we call SetComplete to indicate the transaction's
success and to ensure the data gets committed to the database. The
OnTransactionCommit event fires and the customer is directed to a page
confirming the transactions success and e-mails both them and the order shipping
department.
var sElementKey;
var iItemId;
var iQty;
// Loop through the item form elements
for (var iElementCounter =
iItemElementStart; _
iElementCounter <= Request.Form.Count; iElementCounter++)
{
sElementKey = new
String(Request.Form.Key(iElementCounter));
// if element NAME
starts with ID its an item form element
if
(sElementKey.substr(3,2) == "ID")
{
//
Get ItemId from form VALUE passed
iItemId = parseInt _
(sElementKey.substring(5,sElementKey.length));
//
move to next element which is quantity VALUE for that item
iElementCounter++;
//
get quantity
iQty = parseInt(Request.Form(iElementCounter));
//
set command's parameters
loCommand.Parameters("ItemId") = iItemId;
loCommand.Parameters("Qty") = iQty;
//
execute stored procedure
loCommand.Execute();
//
Any errors?
if
(loCommand.Parameters("RV") != 0)
{
// set iErrorNo, abort transaction and break out of loop
iErrorNo = loCommand.Parameters("RV");
ObjectContext.SetAbort;
break;
}
}
}
}
// only set complete if transaction complete with no
errors
if (iErrorNo == 0)
ObjectContext.SetComplete;
} |
Finally we come to the end of
our order processing script and the catch part of the try statement we set at
the beginning of the code. If an error occurs in our code, the statements here
execute and we are able to store the description in sErrorDescription and abort
the transaction which will lead to the page being redirected to transerror.asp
by OnTransactionAbort.
catch(e)
{
sErrorDescription = e;
ObjectContext.SetAbort;
}
%> |
Save the page as
ProcessOrder.asp.
|
Sponsors





|