473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Shopping Cart not updating until refresh

I am having problems with adding items to my shopping cart. The problem
occures when adding items that already exists in the cart.

When a user adds to cart, they are automatically redirected to the
cart,
where they can update quantities.

If the item already exists in the cart, i want to update the quantity
by adding 1 to the quantity of that item. The code all works fine when
i step thru the code, however when running it without debugging.. the
quantity does
not is not updated until the cart page is refreshed..

I can only think that the record in the cart table has not been
updated, by the time that the cart is being read to load the cart page.
I have tried clearing the cache in between adding items, but this did
not solve the problem.

The AddItem function calls the UpdateItem function if the item already
exists in the cart. Otherwise it continues to add the item.

Here is my code:

'################################################# ###########

Public Function AddItem( _
ByVal CartID As String, _
ByVal ProductID As Integer, _
ByVal ProductDetailID As Integer, _
ByVal Quantity As Integer, _
ByVal SizeID As String, _
ByVal ColourID As String, _
ByVal UnitCost As Decimal) As String

Dim SQL As String

' First test to see if the item already exists in the cart.
' if so then we just need to update the quantity

sql = "SELECT Sum(SC.Quantity) AS Quantity FROM tblShoppingCart AS SC
" & _
"WHERE SC.CartID=@CartID And SC.ProductDetailID=@ProductDetailID
" & _
"GROUP BY SC.ProductDetailID "

Dim paramsG As OleDbParameter() = { _
New OleDbParameter("@CartID", OleDbType.VarWChar, 50), _
New OleDbParameter("@ProductDetailID", OleDbType.Integer)}

paramsG(0).Value = CartID
paramsG(1).Value = ProductDetailID

Dim rdrG As OleDbDataReader
Dim blnUpdate As Boolean
Dim intQuantity As Integer
Try
rdrG = GetData(paramsG, SQL)
rdrG.Read()
If rdrG.HasRows Then
intQuantity = CInt(rdrG("Quantity"))
blnUpdate = True
End If
Catch ex As Exception
Throw New _
Lilipro.WebModules.AppException("Error occurred in
ShoppingCart.AddItem", ex)
Finally
rdrG.Close()
End Try

If blnUpdate Then
UpdateItem(CartID, ProductID, ProductDetailID, intQuantity + 1)
Else
' If item does not exist the add the item
SQL = "INSERT INTO tblShoppingCart " & _
"( CartID, ProductID, ProductDetailID, Quantity, SizeID, ColourID,
UnitCost ) " & _
"VALUES ( @CartID, @ProductID, @ProductDetailID, @Quantity, @SizeID,
@ColourID, @UnitCost )"

Dim rdr As OleDbDataReader
Dim params As OleDbParameter() = { _
New OleDbParameter("@CartID", OleDbType.VarWChar, 50), _
New OleDbParameter("@ProductID", OleDbType.Integer), _
New OleDbParameter("@ProductDetailID", OleDbType.Integer), _
New OleDbParameter("@Quantity", OleDbType.Integer), _
New OleDbParameter("@SizeID", OleDbType.Integer), _
New OleDbParameter("@ColourID", OleDbType.Integer), _
New OleDbParameter("@UnitCost", OleDbType.Decimal)}

params(0).Value = CartID
params(1).Value = ProductID
params(2).Value = ProductDetailID
params(3).Value = Quantity
params(4).Value = SizeID
params(5).Value = ColourID
params(6).Value = UnitCost

Try
rdr = GetData(params, SQL)
rdr.Close()

Catch ex As Exception
Throw New _
Lilipro.WebModules.AppException("Error occurred in
ShoppingCart.AddItem", ex)
End Try
End If

End Function

Public Overloads Function UpdateItem( _
ByVal CartID As String, _
ByVal ProductID As Integer, _
ByVal ProductDetailID As Integer, _
ByVal Quantity As Integer) As String

' throw an exception if quantity is a negative number
If Quantity < 0 Then
Throw New Lilipro.WebModules.AppException("Quantity cannot be a
negative number")
End If

Dim SQL As String
SQL = "UPDATE tblShoppingCart " & _
"SET Quantity=@Quantity " & _
"WHERE CartID=@CartID AND ProductID=@Product AND
ProductDetailID=@ProductDetailID "

' Create Instance of Connection and Command Object
Dim params As OleDbParameter() = { _
New OleDbParameter("@Quantity", OleDbType.Integer), _
New OleDbParameter("@CartID", OleDbType.VarWChar, 50), _
New OleDbParameter("@ProductID", OleDbType.Integer), _
New OleDbParameter("@ProductDetailID", OleDbType.Integer) _
}

params(0).Value = Quantity
params(1).Value = CartID
params(2).Value = ProductID
params(3).Value = ProductDetailID

Try
GetData(params, SQL)

Catch ex As Exception
Throw New Lilipro.WebModules.AppException("Error occurred in
ShoppingCart.UpdateItem ", ex)
Finally

End Try

End Function

'################################################# ###########

How can i get round this?

Nov 19 '05 #1
4 2397
Hi Winshent,

Here is the problem:
rdrG.Read()
If rdrG.HasRows Then
The Read method of a DataReader reads the current row and advances to the
next. If the record has only one row, there are no more rows in the
DataReader; therefore, HasRows = false. Since you're using SUM() in your SQL
Statement, the DataReader will always have 0 or 1 row in it.

The solution is to not call the Read() method at all. You're only interested
in finding out if there is 1 row in the DataReader. No need for a round-trip
to the server to fetch data you don't use.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

"Winshent" <wi*************@yahoo.co.uk> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...I am having problems with adding items to my shopping cart. The problem
occures when adding items that already exists in the cart.

When a user adds to cart, they are automatically redirected to the
cart,
where they can update quantities.

If the item already exists in the cart, i want to update the quantity
by adding 1 to the quantity of that item. The code all works fine when
i step thru the code, however when running it without debugging.. the
quantity does
not is not updated until the cart page is refreshed..

I can only think that the record in the cart table has not been
updated, by the time that the cart is being read to load the cart page.
I have tried clearing the cache in between adding items, but this did
not solve the problem.

The AddItem function calls the UpdateItem function if the item already
exists in the cart. Otherwise it continues to add the item.

Here is my code:

'################################################# ###########

Public Function AddItem( _
ByVal CartID As String, _
ByVal ProductID As Integer, _
ByVal ProductDetailID As Integer, _
ByVal Quantity As Integer, _
ByVal SizeID As String, _
ByVal ColourID As String, _
ByVal UnitCost As Decimal) As String

Dim SQL As String

' First test to see if the item already exists in the cart.
' if so then we just need to update the quantity

sql = "SELECT Sum(SC.Quantity) AS Quantity FROM tblShoppingCart AS SC
" & _
"WHERE SC.CartID=@CartID And SC.ProductDetailID=@ProductDetailID
" & _
"GROUP BY SC.ProductDetailID "

Dim paramsG As OleDbParameter() = { _
New OleDbParameter("@CartID", OleDbType.VarWChar, 50), _
New OleDbParameter("@ProductDetailID", OleDbType.Integer)}

paramsG(0).Value = CartID
paramsG(1).Value = ProductDetailID

Dim rdrG As OleDbDataReader
Dim blnUpdate As Boolean
Dim intQuantity As Integer
Try
rdrG = GetData(paramsG, SQL)
rdrG.Read()
If rdrG.HasRows Then
intQuantity = CInt(rdrG("Quantity"))
blnUpdate = True
End If
Catch ex As Exception
Throw New _
Lilipro.WebModules.AppException("Error occurred in
ShoppingCart.AddItem", ex)
Finally
rdrG.Close()
End Try

If blnUpdate Then
UpdateItem(CartID, ProductID, ProductDetailID, intQuantity + 1)
Else
' If item does not exist the add the item
SQL = "INSERT INTO tblShoppingCart " & _
"( CartID, ProductID, ProductDetailID, Quantity, SizeID, ColourID,
UnitCost ) " & _
"VALUES ( @CartID, @ProductID, @ProductDetailID, @Quantity, @SizeID,
@ColourID, @UnitCost )"

Dim rdr As OleDbDataReader
Dim params As OleDbParameter() = { _
New OleDbParameter("@CartID", OleDbType.VarWChar, 50), _
New OleDbParameter("@ProductID", OleDbType.Integer), _
New OleDbParameter("@ProductDetailID", OleDbType.Integer), _
New OleDbParameter("@Quantity", OleDbType.Integer), _
New OleDbParameter("@SizeID", OleDbType.Integer), _
New OleDbParameter("@ColourID", OleDbType.Integer), _
New OleDbParameter("@UnitCost", OleDbType.Decimal)}

params(0).Value = CartID
params(1).Value = ProductID
params(2).Value = ProductDetailID
params(3).Value = Quantity
params(4).Value = SizeID
params(5).Value = ColourID
params(6).Value = UnitCost

Try
rdr = GetData(params, SQL)
rdr.Close()

Catch ex As Exception
Throw New _
Lilipro.WebModules.AppException("Error occurred in
ShoppingCart.AddItem", ex)
End Try
End If

End Function

Public Overloads Function UpdateItem( _
ByVal CartID As String, _
ByVal ProductID As Integer, _
ByVal ProductDetailID As Integer, _
ByVal Quantity As Integer) As String

' throw an exception if quantity is a negative number
If Quantity < 0 Then
Throw New Lilipro.WebModules.AppException("Quantity cannot be a
negative number")
End If

Dim SQL As String
SQL = "UPDATE tblShoppingCart " & _
"SET Quantity=@Quantity " & _
"WHERE CartID=@CartID AND ProductID=@Product AND
ProductDetailID=@ProductDetailID "

' Create Instance of Connection and Command Object
Dim params As OleDbParameter() = { _
New OleDbParameter("@Quantity", OleDbType.Integer), _
New OleDbParameter("@CartID", OleDbType.VarWChar, 50), _
New OleDbParameter("@ProductID", OleDbType.Integer), _
New OleDbParameter("@ProductDetailID", OleDbType.Integer) _
}

params(0).Value = Quantity
params(1).Value = CartID
params(2).Value = ProductID
params(3).Value = ProductDetailID

Try
GetData(params, SQL)

Catch ex As Exception
Throw New Lilipro.WebModules.AppException("Error occurred in
ShoppingCart.UpdateItem ", ex)
Finally

End Try

End Function

'################################################# ###########

How can i get round this?

Nov 19 '05 #2
Hi Kevin

Thanks for the response.

I need to use the read method to get the quantity of the item. The
quantity is then updated in the function UpdateItem where it accepts
the variable intQuantity
rdrG.Read()
If rdrG.HasRows Then
intQuantity = CInt(rdrG("Quantity"))
blnUpdate = True
End If If blnUpdate Then
UpdateItem(CartID, ProductID, ProductDetailID, intQuantity + 1)
also, if i change the above to read (which i will do - thanks for the
tip)
If rdrG.HasRows Then
rdrG.Read()

I still have the problem of the cart page not reading the updated
quantity for the item. But, when refreshing the cart page, it reads
correctly.. which proves that the update is being triggerred

Nov 19 '05 #3
Not a problem. Just use the following:

If rdrG.Read() Then
intQuantity = CInt(rdrG("Quantity"))
blnUpdate = True
End If

The Read() method returns true if there is data, and false if it is past the
end of the data (or if there is no data).

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

"Winshent" <wi*************@yahoo.co.uk> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi Kevin

Thanks for the response.

I need to use the read method to get the quantity of the item. The
quantity is then updated in the function UpdateItem where it accepts
the variable intQuantity
rdrG.Read()
If rdrG.HasRows Then
intQuantity = CInt(rdrG("Quantity"))
blnUpdate = True
End If

If blnUpdate Then
UpdateItem(CartID, ProductID, ProductDetailID, intQuantity + 1)


also, if i change the above to read (which i will do - thanks for the
tip)
If rdrG.HasRows Then
rdrG.Read()

I still have the problem of the cart page not reading the updated
quantity for the item. But, when refreshing the cart page, it reads
correctly.. which proves that the update is being triggerred

Nov 19 '05 #4
Hi Kevin

i dont think i have explained my problem.. the code above all works
fine.. the record is updated if the item exists. as i say this all
works.. what doesnt work is the code that calls it.

this is the code that calls it

'################################################# ###########

Public Sub AddToCart_Click(ByVal sender As Object, _
ByVal e As System.Web.UI.ImageClickEventArgs) _
Handles AddToCart.Click

'some other code

cart.AddItem(CartID, Request.Params("productid"), ProductDetailID, 1,
_
cboSize.SelectedValue, cboColour.SelectedValue,
CDec(lblUnitCost.Text))

Response.Redirect("ShoppingCart.aspx")

End Sub

'################################################# ###########

As you can see from this.. the item is added to the cart.. whether that
be adding a new record.. or updating the quantity field of an existing
record, it then redirects the user to the ShoppingCart.aspx page.
However, when it follows the route of updating the quantity field of an
existing record, the ShoppingCart.aspx does not reflect the update.

The update only shows when you then refresh the ShoppingCart.aspx
page..

ie.. when adding a product for a second time, the shopping cart page
initially still shows a quantity of 1. When i press refresh, it
displays 2.
Its really weird!!

still dont know what to do about this!!

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert | last post by:
Hello all.. Im a beginner in php and looking to do something similar to a shopping cart system. I want users to be able to view profiles on people and be able to add them to the "shopping cart"...
1
by: madison | last post by:
Hi, I am trying to start a website using paypals shopping cart function. If i have 10 items and they sell out, how do I make it so the item is then listed as sold out. The next person would not...
12
by: Josef Blösl | last post by:
hi folks! i have a cart link in all of my listed products on the site. when i press this link i will generate a url like this ...
2
by: Mark Lordi | last post by:
Hello everyone, I have a site that I just put together http://www.redzcomfort.com everything was working fine up until about 3 days ago when Microsoft released 3 security updates. (KB910437)...
7
by: isaac2004 | last post by:
hi i have a basic asp page that acts as an online bookstore. on my cart page i am having trouble generating 3 numbers; a subtotal, a shipping total, and a final price. here is my code i would...
5
by: TRB_NV | last post by:
I'm losing information from my Session when I change pages or start the same page over again. I simplified the code so the example is really clear. The sample code that follows is supposed to...
1
by: jecha | last post by:
I'm implementing a shopping cart but am having a problem in checking out a person who has added item in his/her shopping busket.The code for the checkout.php script is given below <?...
15
gregerly
by: gregerly | last post by:
Hello, I once again turn to this community of genius' for some help with a problem. I've got a shopping cart (which is working well so far) that I will be implementing all kinds of AJAX...
1
Tarantulus
by: Tarantulus | last post by:
Hi Guys, I'm creating a simple shopping cart app. I have a button which launches $PHP_SELF? followed by variables (product id, qty, price etc) the cart works fine, apart from updating the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.