W
Winshent
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?
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?