Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 12th, 2007, 09:36 AM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP Shopping card problem - Urgent please

<%@LANGUAGE="VBSCRIPT"%>

<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_Profarin_STRING
Recordset1.Source = "SELECT * FROM Produtos"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0
%>
<% ' ***** Begin the functions to be called by the runtime script *****
' To find the actual runtime code scroll WAY DOWN....

' This function is written to enable the adding of multiples of an item
' but this sample always just adds one. If you wish to add different
' quantities simply replace the value of the Querystring parameter count.
' We didn't do this because we wanted to keep the whole thing simple and
' not get into using forms so it stayed relatively readable.
Sub AddItemToCart(iItemID, iItemCount)
     If dictCart.Exists(iItemID) Then
        dictCart(iItemID) = dictCart(iItemID) + iItemCount
    Else
        dictCart.Add iItemID, iItemCount
    End If
    Response.Write "<p>" & iItemCount & " of item # " & iItemID & " have been added to your cart.</p>" & vbCrLf
End Sub

Sub RemoveItemFromCart(iItemID, iItemCount)
     If dictCart.Exists(iItemID) Then
        If dictCart(iItemID) <= iItemCount Then
            dictCart.Remove iItemID
        Else
            dictCart(iItemID) = dictCart(iItemID) - iItemCount
        End If
        Response.Write "<p>" & iItemCount & " of item # " & iItemID & " have been removed from your cart.</p>" & vbCrLf
    Else
        Response.Write "<p>Couldn't find any of that item your cart.</p>" & vbCrLf
    End If
End Sub

Sub ShowItemsInCart()
    Dim objKey
    Dim aParameters ' as Variant (Array)
    Dim sTotal, sShipping

    %>
    <table border="1" cellpadding="3" cellspacing="1">
        <tr>
            <td>Item #</td>
            <td>Description</td>
            <td>Quantity</td>
            <td>Remove Item From Cart</td>
            <td>Price</td>
            <td>Totals</td>
        </tr>
    <%
    sTotal = 0
    For Each objKey in dictCart
        aParameters = GetItemParameters(objKey)
        %>
        <tr>
            <td align="center"><%= objKey %></td>

            <td align="left"><%= aParameters(1) %></td>

            <td align="center"><%= dictCart(objKey) %></td>
            <td align="left"><a href="shopping.asp?action=del&item=<%= objKey %>&count=1">Remove One</a>&nbsp;&nbsp;<a href="shopping.asp?action=del&item=<%= objKey %>&count=<%= dictCart(objKey) %>">Remove All</a></td>
            <td align="right">$<%= aParameters(2) %></td>
            <td align="right">$<%= FormatNumber(dictCart(objKey) * CSng(aParameters(2)), 2) %></td>
        </tr>
        <%
        sTotal = sTotal + (dictCart(objKey) * CSng(aParameters(2)))
    Next

    ' Calculate shipping
    sShipping = CalculateShippingCost(sTotal)

    ' Add shipping to total
    sTotal = sTotal + sShipping
    %>
    <tr><td colspan="5" align="right"><b>S+H:</b></td><td align="right">$<%= FormatNumber(sShipping, 2) %></td></tr>
    <tr><td colspan="5" align="right"><b>Total:</b></td><td align="right">$<%= FormatNumber(sTotal, 2) %></td></tr>
    </table>
    <%
End Sub

Sub ShowFullCatalog()
    Dim aParameters ' as Variant (Array)
    Dim I
    Dim iItemCount ' Number of items we sell

    ' If you are really going to use this sample this should probably be pulled from a DB
    iItemCount = 3
    %>
    <table border="1" cellpadding="3" cellspacing="1">
        <tr>
            <td>Image</td>
            <td>Description</td>
            <td>Price</td>
            <td>Add Item To Cart</td>
        </tr>
    <%
    While (NOT Recordset1.EOF)
' For I = 1 to iItemCount
        aParameters = GetItemParameters(I)
        %>
        <tr>
<!-- <td><img src="<'%= aParameters(0) %>" /></td>
            <td><'%= aParameters(1) %></td>
            <td>$<'%= aParameters(2) %></td>-->
        <td><%=(Recordset1.Fields.Item("Cod_Art").Value)%> </td>
            <td><%=(Recordset1.Fields.Item("Nome_Art").Value)% ></td>


<td><a href="shopping.asp?action=add&item=<%=(Recordset1. Fields.Item("Cod_Art").Value)%>&count=1">Add this to my cart!</a></td>
        </tr>
        <%

' Next 'I
    Recordset1.MoveNext()
    Wend%>
    </table>
    <%
End Sub

Sub PlaceOrder()
    Dim objKey
    Dim aParameters ' as Variant (Array)
    Dim sTotal, sShipping

    %>
    <table border="1" cellpadding="3" cellspacing="1">
        <tr>
            <td>Item #</td>
            <td>Description</td>
            <td>Quantity</td>
            <td>Price</td>
            <td>Totals</td>
        </tr>
    <%
    sTotal = 0
    For Each objKey in dictCart
        aParameters = GetItemParameters(objKey)
        %>
        <tr>
            <td align="center"><%= objKey %></td>
            <td align="left"><%= aParameters(1) %></td>
            <td align="center"><%= dictCart(objKey) %></td>
            <td align="right">$<%= aParameters(2) %></td>
            <td align="right">$<%= FormatNumber(dictCart(objKey) * CSng(aParameters(2)), 2) %></td>
        </tr>
        <%
        sTotal = sTotal + (dictCart(objKey) * CSng(aParameters(2)))
    Next

    ' Calculate shipping
    sShipping = CalculateShippingCost(sTotal)

    ' Add shipping to total
    sTotal = sTotal + sShipping
    %>
    <tr><td colspan="4" align="right"><b>S+H:</b></td><td align="right">$<%= FormatNumber(sShipping, 2) %></td></tr>
    <tr><td colspan="4" align="right"><b>Total:</b></td><td align="right">$<%= FormatNumber(sTotal, 2) %></td></tr>
    </table>
    <%

    ' You could also do whatever other processing you would need to here.
    ' For example, send credit card info to processor or send order details
    ' to your warehouse for shipping. I'm just gonna send an email with
    ' the product details so you can see how you'd access them. Please
    ' be aware that email is NOT SECURE! So please don't transfer users'
    ' personal or credit card information this way.
    Dim objMessage, strMessageBody

    strMessageBody = "Quantity Item# Description Unit Price Total" & vbCrLf & vbCrLf
    For Each objKey in dictCart
        aParameters = GetItemParameters(objKey)
        strMessageBody = strMessageBody & " " & dictCart(objKey)
        strMessageBody = strMessageBody & " " & objKey
        strMessageBody = strMessageBody & " " & aParameters(1)
        strMessageBody = strMessageBody & String(25 - Len(aParameters(1)), " ") & aParameters(2)
        strMessageBody = strMessageBody & " " & FormatNumber(dictCart(objKey) * CSng(aParameters(2)), 2) & vbCrLf
    Next
    strMessageBody = strMessageBody & vbCrLf
    strMessageBody = strMessageBody & String(41, " ") & "Shipping: " & FormatNumber(sShipping, 2) & vbCrLf
    strMessageBody = strMessageBody & vbCrLf
    strMessageBody = strMessageBody & String(37, " ") & "Order Total: " & FormatNumber(sTotal, 2) & vbCrLf

    'Response.Write "<pre>" & strMessageBody & "</pre>"

    Set objMessage = Server.CreateObject("CDO.Message")
    With objMessage
        ' Set message attributes
        .To = "Full Name <user name@some domain.com>"
        .From = "Full Name <user name@some domain.com>"
        .Subject = "Shopping Cart Contents"
        .TextBody = strMessageBody
        ' Send message - uncomment the following line only
        ' AFTER you've entered appropriate To and From
        ' addresses above. Then the script will actually
        ' send the messages.
        '.Send
    End With
    Set objMessage = Nothing

End Sub

' We implemented this this way so if you attach it to a database you'd only need one call per item
Function GetItemParameters(iItemID)
    Dim aParameters ' Will contain 3 string values : image path, description, price
                    ' However we need to keep price so it can be converted to a
                    ' single for computation hence no currency symbol. This array
                    ' can also be expanded to contain any other information about the
                    ' product that you might want to pull from the DB.
    Select Case iItemID
        Case 1
            aParameters = Array("images/shop_shirt.gif", "ASP 101 T-Shirt", "15.00")
        Case 2
            aParameters = Array("images/shop_kite.gif", "ASP 101 Kite", "17.50")
        Case 3
            aParameters = Array("images/shop_watch.gif", "ASP 101 Watch", "35.00")
        Case 4 ' Not in use because we couldn't draw a pen in a few seconds!
            aParameters = Array("images/shop_pen.gif", "ASP 101 Pen", "5.00")
    End Select

    ' Return array containing product info.
    GetItemParameters = aParameters
End Function

' Free shipping if there's nothing to ship or the merchandise
' subtotal is over $100. Otherwise, small orders are $5 and
' larger orders are $7.50.
Function CalculateShippingCost(sngOrderTotal)
    Dim sngShipping

    If sngOrderTotal <= 0 Or sngOrderTotal > 100 Then
        sngShipping = 0
    ElseIf sngOrderTotal < 50 Then
        sngShipping = 5
    Else
        sngShipping = 7.50
    End If

    CalculateShippingCost = sngShipping
End Function
%>


<% ' ***** Begin the infamous runtime script *****
' Declare our Vars
Dim dictCart ' as dictionary
Dim sAction ' as string
Dim iItemID ' as integer
Dim iItemCount ' as integer

' Get a reference to the cart if it exists otherwise create it
If IsObject(Session("cart")) Then
    Set dictCart = Session("cart")
Else
    ' We use a dictionary so we can name our keys to correspond to our
    ' item numbers and then use their value to hold the quantity. An
    ' array would also work, but would be a little more complex and
    ' probably not as easy for readers to follow.
    Set dictCart = Server.CreateObject("Scripting.Dictionary")
End If

' Get all the parameters passed to the script
sAction = CStr(Request.QueryString("action"))
iItemID = CInt(Request.QueryString("item"))
iItemCount = CInt(Request.QueryString("count"))

%>
<table border="0" cellspacing="0" cellpadding="0">
<tr><td>
<%
' Select action based on user input
Select Case sAction
    Case "add"
        AddItemToCart iItemID, iItemCount
        ShowItemsInCart
        %>
        </td></tr>
        <tr><td align="right">
        <a href="shopping.asp?action="><img src="images/shop_look.gif" border="0" width="46" height="46" alt="Continue Looking" /></a>
        <a href="shopping.asp?action=checkout"><img src="images/shop_checkout.gif" border="0" width="46" height="46" alt="Checkout" /></a>
        <%
    Case "del"
        RemoveItemFromCart iItemID, iItemCount
        ShowItemsInCart
        %>
        </td></tr>
        <tr><td align="right">
        <a href="shopping.asp?action="><img src="images/shop_look.gif" border="0" width="46" height="46" alt="Continue Looking" /></a>
        <a href="shopping.asp?action=checkout"><img src="images/shop_checkout.gif" border="0" width="46" height="46" alt="Checkout" /></a>
        <%
    Case "viewcart"
        ShowItemsInCart
        %>
        </td></tr>
        <tr><td align="right">
        <a href="shopping.asp?action="><img src="images/shop_look.gif" border="0" width="46" height="46" alt="Continue Looking" /></a>
        <a href="shopping.asp?action=checkout"><img src="images/shop_checkout.gif" border="0" width="46" height="46" alt="Checkout" /></a>
        <%
    Case "checkout"
        PlaceOrder
        %>
        </td></tr>
        <tr><td align="left">
        <br /><br />
        <h3>Thank you for your order!</h3>
        <p>
        If this had been an actual shopping cart, this is where you would have had them enter
        their personal information and payment method and then taken care of any backend
        processing before finalizing their order. However as this is a shopping cart sample
        and this is where security becomes a problem, we'll leave it for a future sample.
        </p>
        <%
    Case Else ' Shop
        ShowFullCatalog
        %>
        </td></tr>
        <tr><td align="right">
        <a href="shopping.asp?action=viewcart"><img src="images/shop_cart.gif" border="0" width="46" height="46" alt="View Cart Contents" /></a>
        <%
End Select

' Return cart to Session for storage
Set Session("cart") = dictCart
%>
</td></tr>
</table>
<%
Recordset1.Close()
Set Recordset1 = Nothing
%>


Hello, i have this problem, i copy this code from a site and in this section Sub ShowFullCatalog() try to apply my database fields and it worked.
The problem is when i tried to pass it to Sub ShowItemsInCart() (i think) i get and error Excessiv of capacity: 'CInt' but i try to replace for cstr and the error was the some Excessiv of capacity: 'CInt' (yes it was cint againt).
Please help
Thenk you



  #2 (permalink)  
Old June 12th, 2007, 06:42 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

You will always run into problems copying other peoples code. You need to understand what every line of code does to work on a page. Then when you run into a problem, paste the problematic code ONLY. You will get a great deal of help here doing this.

Constructivly - What you have asked, the way you asked it is not what forums are about.

Wind is your friend
Matt


Similar Threads
Thread Thread Starter Forum Replies Last Post
WroxUnited shopping card password mahaderway BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 4 December 10th, 2006 01:31 PM
ASP.NET 2.0 Email Problem-Urgent sohrabus ASP.NET 2.0 Professional 3 October 6th, 2006 11:19 PM
ASP credit card p2ptolu Classic ASP Basics 2 March 29th, 2006 08:33 PM
Card shuffling problem! the Real Newman Java Basics 2 January 30th, 2006 12:57 PM
Com Interoperability problem(ASP.NET)Urgent deeptisingh20 General .NET 2 January 19th, 2006 08:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.