Shopping Cart not updating until refresh - Access 2K backend

  • Thread starter Thread starter Winshent
  • Start date Start date
W

Winshent

This is my first shopping cart i have written and i have a problem when
adding items that already exists in the cart.

When a user adds to cart, they 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. 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.

How can i get round this problem?


Here is my code:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Page.IsPostBack = False Then
GetShoppingCartList()
End If

End Sub

Sub GetShoppingCartList()

Dim cart As ChoicesYours.WebModules.Commerce.ShoppingCart = _
New ChoicesYours.WebModules.Commerce.ShoppingCart _

(System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))

' Obtain current user's shopping cart id
Dim cartId As String = cart.GetShoppingCartId()

' If no items, hide details and display message
If cart.GetItemCount(cartId) = 0 Then
'DetailsPanel.Visible = False
lblError.Text = "There are currently no items in your shopping
cart."
Else
' Databind Gridcontrol with Shopping Cart Items
Dim rdr As OleDbDataReader
rdr = cart.GetItems(cartId)
MyList.DataSource = rdr
MyList.DataBind()
rdr.Close()
'Update Total Price Label
'lblTotal.Text = String.Format("{0:c}", cart.GetTotal(cartId))
End If

End Sub


Public Sub btnUpdateCart_Click(ByVal sender As System.Object, _
ByVal e As System.Web.UI.ImageClickEventArgs) Handles
btnUpdateCart.Click
UpdateShoppingCartDatabase()
GetShoppingCartList()
End Sub


Sub UpdateShoppingCartDatabase()

Dim cart As ChoicesYours.WebModules.Commerce.ShoppingCart = _
New ChoicesYours.WebModules.Commerce.ShoppingCart _

(System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))

' Obtain current user's shopping cart id
Dim cartId As String = cart.GetShoppingCartId()

' Iterate through all rows within shopping cart list
Dim i As Integer
For i = 0 To MyList.Items.Count - 1

' Obtain references to row's controls
Dim quantityTxt As TextBox =
CType(MyList.Items(i).FindControl("Quantity"), TextBox)
Dim remove As CheckBox =
CType(MyList.Items(i).FindControl("Remove"), CheckBox)

' Wrap in try/catch block to catch errors in the event that someone
types in
' an invalid value for quantity
Dim quantity As Integer
Try
quantity = CInt(quantityTxt.Text)

' If the quantity field is changed or delete is checked
If quantity <> CInt(MyList.DataKeys(i)) Or remove.Checked = True
Then

Dim lblProductID As Label =
CType(MyList.Items(i).FindControl("ProductID"), Label)
Dim lblProductDetailID As Label =
CType(MyList.Items(i).FindControl("ProductDetailID"), Label)

If quantity = 0 Or remove.Checked = True Then
cart.RemoveItem(cartId, CInt(lblProductID.Text),
CInt(lblProductDetailID.Text))
Else
cart.UpdateItem(cartId, CInt(lblProductID.Text),
CInt(lblProductDetailID.Text), _
quantity)
End If
End If
Catch ex As Exception
Throw New Lilipro.WebModules.AppException _
("Error occured in
ShoppingCartASPXVB.UpdateShoppingCartDatabase ", ex)
End Try
Next
End Sub
 
Whoops!!

This is the relevant 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
 
Back
Top