• Books
 •
DHTML
 •
Downloads
 •
Links
 •
Scripts
 •
Tutorials
 


Home

Roll with it – Degrading Gracefully When It All Goes Wrong

Page 19 of 22 - Chapter 14

We have not finished with the ProcessOrder.asp page yet: we need to add some code to handle the out of stock error (547) in a more user friendly way then just redirecting to an error page.

The out of stock problem is dealt with by displaying a list of the order items not available in sufficient quantity to fulfill the order and letting the user choose to either cancel the order completely or proceed with the amounts available. If the order was successful, or any error other than out of stock occurred, then the user will be redirected to another page elsewhere and will never see the HTML we are about to create.

The ASP page will be processed in totality even if re-direction occurs, so, to avoid the overhead of the order failed code, we nest it inside an if statement. Place the code at the bottom of the page immediately following the code we just created.

<HTML>
<BODY>
<%
   // If error is Out Of Stock then create HTML
   if (iErrorNo == 547)
   {
%>

The next task is to create a form replicating the form elements originally passed when the customer submitted their order – the last thing they want to do is be retyping the information. The form action returns to this page for reprocessing, this time hopefully with sufficient stock (though it is possible others may place orders whilst this customer is thinking about what to do).

<H2>Sorry we are unable to fully satisfy your order</H2>
<P>
<FORM ACTION="ProcessOrder.asp" method="POST">
   We do not currently have sufficent stock for some items in your order,
   details listed below<BR>
   <!-- Name/Address Details -->
   <INPUT TYPE="HIDDEN" NAME="txtTitle"
          VALUE="<%=Request.Form("txtTitle")%>">
   <INPUT TYPE="HIDDEN" NAME="txtFirstName"
          VALUE="<%=Request.Form("txtFirstName")%>">
   <INPUT TYPE="HIDDEN" NAME="txtLastName"
          VALUE="<%=Request.Form("txtLastName")%>">
   <INPUT TYPE="HIDDEN" NAME="txtEmail"
          VALUE="<%=Request.Form("txtEmail")%>">
   <INPUT TYPE="HIDDEN" NAME="txtStreet"
          VALUE="<%=Request.Form("txtStreet")%>">
   <INPUT TYPE="HIDDEN" NAME="txtCity"
          VALUE="<%=Request.Form("txtCity")%>">
   <INPUT TYPE="HIDDEN" NAME="txtLocality"
          VALUE="<%=Request.Form("txtLocality")%>">
   <INPUT TYPE="HIDDEN" NAME="txtPostCode"
          VALUE="<%=Request.Form("txtPostCode")%>">
   <INPUT TYPE="HIDDEN" NAME="txtCountry"
          VALUE="<%=Request.Form("txtCountry")%>">
   <!-- Credit Card Details -->
   <INPUT TYPE="HIDDEN" NAME="txtCCHolderName"
          VALUE="<%= Request.Form("txtCCHolderName") %>">
   <INPUT TYPE="HIDDEN" NAME="txtCCNo"
          VALUE="<%= Request.Form("txtCCNo") %>">
   <INPUT TYPE="HIDDEN" NAME="txtCCType"
          VALUE="<%= Request.Form("txtCCType") %>">
   <INPUT TYPE="HIDDEN" NAME="txtCCExpire"
          VALUE="<%= Request.Form("txtCCExpire") %>">


We need to create a list of the items the customer ordered and check the availability of each item. In order to do so, we use the ItemAvailability stored procedure to retrieve the quantities available. Then we create a new command object which will be reused to execute the stored procedure as we loop though the shopping basket's items.

<%
   var loRS;
   // Create new command object
   loCommand = null;
   loCommand = Server.CreateObject("ADODB.Command");
   loCommand.CommandText = "ItemAvailability";
   loCommand.CommandType = adCmdStoredProc;
   loCommand.Name = "ItemAvailability";
   //@ItemId int
   loParam =loCommand.CreateParameter("ItemId", adInteger, adParamInput);
   loCommand.Parameters.Append(loParam);
   loCommand.ActiveConnection = loConn;

Next we start looping though the item elements in the form, retrieving the ItemId and quantity and using this information to access the database with the Command object and find out how many we actually have in stock.

var sElementKey;
   var iItemId;
   var iQty;
   var iQtyInStock;
   for (var iElementCounter = iItemElementStart; _
        iElementCounter <= Request.Form.Count; iElementCounter++)
   {
      sElementKey = new String(Request.Form.Key(iElementCounter));
      // If this is an item element
      if (sElementKey.substr(3,2) == "ID")
      {
         // get ItemId
         iItemId = parseInt(sElementKey.substring(5,sElementKey.length));
         iElementCounter++;
         // Get desired quantity
         iQty = parseInt(Request.Form(iElementCounter));
         // access database to see how many are actually available
        loCommand.Parameters("ItemId") = iItemId;
        loRS = loCommand.Execute();
        iQtyInStock = loRS("QtyInStock");

If we find that there is insufficient stock available for the item we need to let the customer know. If there is no stock at all then we tell the user, otherwise we tell them how many are actually available. We also create hidden form elements to store the ItemId and quantities actually available. If the user goes ahead with the quantities available then this information can be retrieved after the form post.

 

Sponsors

The Internets address book

The New Way to Download

Web Hosting starting at $10 / month!

Free Animations







Home  

© 2000 Iain Hendry