Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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
 
Old April 9th, 2007, 08:40 AM
Authorized User
 
Join Date: Sep 2006
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default Shopping Cart Query

Hi,

I'm finalising an e-commerce based web site written in asp.net 1.1. using vb.net. Whilst testing the functionality I've noted that if a customer adds the same item twice, the shopping cart (which is a datagrid) displays both items as separate rows within the datagrid.

To improve this I would like to put an if statement in to say if the productID is the same, increase the quantity by one. From a coding perspective can you advise how I would exactly do this ? I've included some sample code from the "Add To Cart" sub procedure for reference :

Code:
 Sub AddToCart(sender As Object, e As EventArgs)

        'Define CartID
        Dim CartID As Double

        'Passes ProductID as sender
        Dim intProductID As Integer = sender.CommandArgument'<%# DataBinder.Eval(Container.DataItem, "intProductID")%>'

        'Set quantity value
        Dim Quantity As Integer
        Quantity = 1

        'Define intQuantityInStock and reference to CheckQuantity function
        Dim intQuantityInStock As Integer = CheckQuantity(intProductID)

        'Check enough quantity in stock before adding to cart

        'Label used for testing purposes
        lblQuantity.Text = quantity
        lblQuantity.Visible = False

        'If value intQuantityInStock is less than quantity value
        If intQuantityInStock < quantity
            'Display low in stock error message
            lblError.Text = "This item is currently low in stock. Please select an alternative</b>"
            lblError.Visible = True
        End If


        'Set date and time
        Dim dtNow as Date = Date.Now
        Dim ts as String
        ts = dtNow.ToString("dd/MM/yyyy"  & " " & "HH:mm:ss")

        'Updates CartItems table in db
        UpdatetblCartItems(Session("CartID"),Quantity,intProductID,ts)

        'Goto shopping cart page
         Response.Redirect("cart.aspx")
     End Sub
 Any online examples would help - thanks.


 
Old April 9th, 2007, 08:48 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I assume this method is the one you are going to want to look at

UpdatetblCartItems(Session("CartID"),Quantity,intP roductID,ts)

It is here, again I assume, that you are writing values to a database which in turn is bound to the datagrid, am I correct?

If so, it seems that when that method is called, you simply fire a SQL INSERT statement (or stored procedure) that adds the item information to the table. To solve your problem, you could approach it 2 different ways, IMHO, and they are as follows:

1)You could run a SELECT count([value]) from shoppingcart where pID = [productID to add] then ExecuteScalar() to return the value. If the value returned is 0 you know that you then need to insert a new row but if n > 0 then you know that you need to call an update statement.

2)You could write a stored procedure that handled this logic for you and the decision to update or insert would be made in SQL Server and not in your code.

Personally, I would use the second option because it cuts out an additional round trip to SQL while still providing the same functionality. hth.


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 9th, 2007, 09:36 AM
Authorized User
 
Join Date: Sep 2006
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks - would the second option still be relevant for an access database, as is the case here ?



 
Old April 9th, 2007, 09:48 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I haven't used access in quite a few years, but I do know that you can't use stored procedures inside of Access. However, I am not sure if you could achieve the same effect by writing and saving a query inside of access and calling that query from your code.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 10th, 2007, 01:32 PM
Authorized User
 
Join Date: Sep 2006
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok - thanks for the advice






Similar Threads
Thread Thread Starter Forum Replies Last Post
Shopping Cart seannie ASP.NET 2.0 Basics 0 December 12th, 2006 10:28 AM
Shopping cart help rsm42 ASP.NET 1.0 and 1.1 Basics 3 December 9th, 2006 06:09 AM
Shopping Cart inkrajesh ASP.NET 1.0 and 1.1 Basics 2 February 28th, 2006 03:08 AM
shopping cart xipnl BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 June 10th, 2005 07:00 PM
Shopping Cart query Adam H-W Classic ASP Basics 7 January 19th, 2004 02:23 PM





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