Record Not Updated Until Page Refreshed

W

Winshent

I am having problems with adding items to my shopping cart.

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

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.

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

I can only think that the record in the cart table has not been updated
by the time that the cart table 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.

Here is my code:

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


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


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)
If rdrG.HasRows Then
rdrG.Read()
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

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


Its really weird!! How can i get round this?
 
W

Winshent

Hi Jignesh

Thanks for responding..

The AddToCart_Click method is for a button on my Products.aspx page..
It triggers the AddItem code which then updates the database. So yes
the UpdateItem function is also updating the database.

The user is then redirected to the ShoppingCart.aspx page. This page
then reads from the database.

I have managed to make progress by adding a sleep before redirecting.
This allows time for the the update to run on the database before
redirecting to the Cart page. However, this obviously isnt the desired
solution.

Any ideas?

The code now reads :




Public Sub AddToCart_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs) _
Handles AddToCart.Click
cart.AddItem(CartID, Request.Params("productid"), ProductDetailID, 1,
cboSize.SelectedValue, cboColour.SelectedValue, CDec(lblUnitCost.Text))

Threading.Thread.Sleep(500)

Response.Redirect("ShoppingCart.aspx")

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top