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
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