Source Code for http://194.8.136.100/XSB/bag.asp

COLOR LEGEND
Server Side Script
Client Side Script
Hyperlink
Include
Frames
Comments
Object Code Link
Standard HTML and Text


<%  Response.Buffer = True %>
<html>
<head>
   <title>Implementing the Shopping Bag</title>
</head>
<body bgcolor="#ffffff">
<%
' get or create the bag
Set myBag = LoadXSB()

' add or modify products
If Request.ServerVariables("CONTENT_LENGTH") > 0 Then
   Select Case Trim(Request.Form("DO"))
     Case "Shop for more"
       Response.Redirect "products.asp"
       Response.End
     Case "Recalculate"
       RecalcBag myBag
     Case "Check out"
       ' recalculate and then redirect to checkout
   End Select
Else
   ' products are added using the query string
   nProductId = Trim(Request.QueryString("id"))
   AddtlProduct nProductId, myBag
End If

Session("BagItems") = myBag.SaveBag

If myBag.LineItems.Count > 0 Then
   PrintBag myBag
Else
   Response.Write "Sorry, you have no items in your bag!"
End If
%>

</body>
</html>

<SCRIPT  LANGUAGE=VBSCRIPT RUNAT=SERVER>
Function LoadXSB()
   Set myBag = Server.CreateObject("SoftwingXSB.ShoppingBag")
   If Not IsEmpty(Session("BagItems")) Then
     myBag.LoadBag CStr(Session("BagItems"))
   Else
     myBag.InitBag
   End If
   Set LoadXSB = myBag
End Function

Sub AddtlProduct(nProductId, myBag)
   bValid = True
  
   If "" = nProductId Or Not IsNumeric(nProductId) Then
     bValid = False
   Else
     Set objConn = Server.CreateObject("ADODB.Connection")
     objConn.Open "provider=sqloledb.1;user id=sa;password=;initial catalog=Northwind;data source=TOPGUN"
     Set objRS = Server.CreateObject("ADODB.Recordset")
     Set objRS.ActiveConnection = objConn
     objRS.Open "SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products WHERE ProductId=" & nProductId
     If objRS.EOF And objRS.BOF Then
       bValid = False
     Else
       avarProduct = objRS.GetRows()
     End If
     objConn.Close
     Set objRS = Nothing
     Set objConn = Nothing
   End If
  
   ' if the product was ok, add it to the bag
   If bValid Then
     ' first check if the product isn't already in the bag
     If Not myBag.IsProductInBag(nProductId) Then
       myBag.LineItems.AddLineItem avarProduct(0,0), Server.HtmlEncode(avarProduct(1,0)), _
         Server.HtmlEncode(avarProduct(2,0)), avarProduct(3,0), 1
     End If
   End If
End Sub

Sub Out(strOut)
   Response.Write strOut
End Sub

Sub PrintBag(myBag)
   Out "<FORM METHOD=""POST"" ACTION="""
   Out Request.ServerVariables("SCRIPT_NAME") & """>"
   Out "<TABLE><TR><TH>Units</TH><TH COLSPAN=2>"
   Out "Product</TH><TH>Unit Price</TH><TH>Price</TH>"
   Out "</TR>" & vbCrLf
   For Each item In myBag.LineItems
     Out "<TR><TD><input type=""TEXT"" name=""Qty"
     Out item.ProductId
     Out """ VALUE=""" & item.QuantityInBag & """ SIZE=""3"">"
     Out "<TD>" & item.ProductName
     Out "</TD><TD>" & item.QuantityPerUnit & "</TD>"
     Out "<TD>" & FormatCurrency(item.PricePerUnit) & "</TD>"
     Out "<TD><B>" & FormatCurrency(item.Total) & "</B></TD>"
     Out "</TR>" & vbCRLF
   Next
   Out "<TR><TD COLSPAN=5><HR></TD></TR>"
   Out "<TR><TD COLSPAN=3></TD><TD><B>Total:</B></TD>"
   Out "<TD><B>" & FormatCurrency(myBag.Total) & "</B></TD></TR>" & vbCrLf
   Out "<TR><TD COLSPAN=5 ALIGN=""RIGHT"">"
   Out "<INPUT TYPE=SUBMIT NAME=""DO"" VALUE=""Shop for more"">&nbsp;"
   Out "<INPUT TYPE=SUBMIT NAME=""DO"" VALUE=""Recalculate"">&nbsp;"
   Out "<INPUT TYPE=SUBMIT NAME=""DO"" VALUE=""Check out"">"
   Out "</TD></TR></TABLE></FORM>"
End Sub

Sub RecalcBag(myBag)
   For Each item in myBag.LineItems
     nQty = Trim(Request.Form("Qty" & item.ProductId))
     If "" <> nQty And IsNumeric(nQty) Then
       If nQty <  1 Then
         myBag.LineItems.Remove item.ProductId
       Else
         item.QuantityInBag = nQty
       End If
     End If
   Next
End Sub
</SCRIPT>