Problem with calculating and displaying totals

easyeman
01-03-2007, 01:17 PM
I'm having a slight problem and no idea why it's doing it. I have a shopping cart page that includes items people purchase on the site, and there's an update button and checkout button. There is a remove checkbox and quantity box so customers can update quantity and such from that page. The problem I am having is that for some reason the Total that displays on the page wants to double the total when update is pressed. However, it does not actually calculate it as double, so if a customer checks out then they'll be charged the correct amount. It's just odd that for some reason the Total wants to show double of the items when updated...doesn't keep doubling but just shows double of the total. It used to not do that and I've tried to fix it but can't figure out where it's doing it at.

So I'm wondering if anyone has any clue why it would do that...it's not a major deal since it is calculating the right totals, but it can be confusing for customers since it shows the total being double on that page when a quantity is updated. I'll post a picture of what the screen looks like below and the code I have for the function used to Update the quantity. Not sure if that's where it is but I can't think of anything else. If anyone can help I'd really appreciate it.

http://i5.photobucket.com/albums/y165/easyeman/checkout.jpg

'*******************************************************
' The UpdateShoppingCartDatabase helper method is used to
' update a user's items within the shopping cart database
' using client input from the GridControl.
'*******************************************************

Sub UpdateShoppingCartDatabase()

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

If quantity = 0 Or remove.Checked = True Then
cart.RemoveItem(cartId, Convert.ToString(lblProductID.Text))
Else
cart.UpdateItem(cartId, Convert.ToString(lblProductID.Text), quantity)
End If
End If
Catch
MyError.Visible = True
MyError.Text = "There has been a problem with one or more of your inputs."
End Try
Next
End Sub

wayneph
01-04-2007, 07:21 AM
My guess is that you're not initializing the value, but since you don't show us the code where the total is calculated, I'm just guessing.

When the page loads the first time, the label has a 0, so the first time through the total is correct. When you click Update, it starts from the initial total and goes up from there. If you click update again, I would guess that you would actually triple the total.

If that's not it, can you show us the code where you actually caculate the total.

easyeman
01-04-2007, 01:25 PM
My guess is that you're not initializing the value, but since you don't show us the code where the total is calculated, I'm just guessing.

When the page loads the first time, the label has a 0, so the first time through the total is correct. When you click Update, it starts from the initial total and goes up from there. If you click update again, I would guess that you would actually triple the total.

If that's not it, can you show us the code where you actually calculate the total.

yeah, I'm sorry I guess I wasn't thinking or didn't want to bug you down with too much code.

Here's all the code used for the processing of checkout, so hopefully it is in here somewhere, but I posted it all since it may help you see what it is doing.

And to respond to your question, it's not doubling it each time which is puzzling me, it does it once but then each time you click "update" or refresh the page, it'll just show double, BUT if you click the cart button at the top of the page it goes back to normal...so I'm guessing it's just something within the Update function that I cannot find. Oh well, here's all the code for the code behind.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyList.Height = System.Web.UI.WebControls.Unit.Pixel(10)

If Not Request.Params("Error") = Nothing Then
If Request.Params("Error") = "1" Then
InValid.Text = "One Or More Items Were Not Added Because They Were Invalid"
InValid.Visible = True
End If
End If

Response.Cache.SetCacheability(HttpCacheability.NoCache)

' Populate the shopping cart the first time the page is accessed.
If Page.IsPostBack = False Then
PopulateShoppingCartList()
End If

End Sub

'*******************************************************
' The UpdateBtn_Click event is raised when a user clicks
' the "update" button on the client. The event handler
' updates all items in the cart back to the database,
' and then repopulates the datagrid with the current
' cart contents.
'*******************************************************

Sub UpdateBtn_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles UpdateBtn.Click
' Update the Shopping Cart and then Repopulate the List
UpdateShoppingCartDatabase()
PopulateShoppingCartList()
End Sub

'*******************************************************
' The CheckoutBtn_Click event is raised when a user clicks
' the "checkout" button on the client. The event handler
' updates all items in the cart back to the database,
' and then redirects the user to the checkout.aspx page
'*******************************************************

Sub CheckoutBtn_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles CheckoutBtn.Click
' Update Shopping Cart
UpdateShoppingCartDatabase()

' If the cart isn't empty, navigate to checkout page
If cart.GetItemCount(cartId) <> 0 Then

'===================================================================== ===========
'TESII -12/13/05
'Modification made to allow checkout for Punchout Portal
'-START
If context.Request.Cookies("NAFECO_Buyercookie") Is Nothing Then
Response.Redirect("~/CheckOut/SelectPayment.aspx")
Else
Response.Redirect("~/Punchout/Confirmation.aspx")
End If
'-END
'===================================================================== ===========
Else
MyError.Visible = True
MyError.Text = "You can't proceed to the Check Out page with an empty cart."
End If

End Sub

'*******************************************************
' The PopulateShoppingCartList helper method is used to
' dynamically populate a GridControl with the contents of
' the current user's shopping cart.
'*******************************************************
Sub PopulateShoppingCartList()

' If no items, hide details and display message
If cart.GetItemCount(cartId) = 0 Then
MyList.Visible = False
UpdateBtn.Visible = False
CheckoutBtn.Visible = False
CartEmpty.Visible = True
CartEmpty.Text = "<html><body><p><font face='Arial'><strong>Your Shopping Cart Is Empty.</strong></font></p><p><font face='Arial' size='2'>To add something in your shopping cart:</font></p><ul><li><font face='Arial' size='2'>Browse from the <a href='http://www.nafeco.com'>homepage</a> by clicking on any of the products in the menu</font></li><li><font face='Arial' size='2'>Enter a NAFECO stock number in the Search field in the header above</font></li></ul><p><font face='Arial' size='2'>Need help finding something you're looking for? Contact Customer Service for superior assistance at <strong>1-800-628-6233</strong>.</font></p></body></html>"
Else
' Databind Gridcontrol with Shopping Cart Items
If Not Convert.ToString(Session("LoginID")) = "" Then
MyList.DataSource = cart.GetItems(cartId, Convert.ToString(Session("LoginID")))
Else
MyList.DataSource = cart.GetItems(cartId)
End If
MyList.DataBind()
End If

End Sub

'*******************************************************
' The UpdateShoppingCartDatabase helper method is used to
' update a user's items within the shopping cart database
' using client input from the GridControl.
'*******************************************************

Sub UpdateShoppingCartDatabase()

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

If quantity = 0 Or remove.Checked = True Then
cart.RemoveItem(cartId, Convert.ToString(lblProductID.Text))
Else
cart.UpdateItem(cartId, Convert.ToString(lblProductID.Text), quantity)
End If
End If
Catch
MyError.Visible = True
MyError.Text = "There has been a problem with one or more of your inputs."
End Try
Next
End Sub

Sub ComputeSum(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim rowTotal As Decimal = Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "ExtendedAmount"))
OrderTotal += rowTotal
If Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Subitem")) = "Y" Then
e.Item.BackColor = Color.Cornsilk
End If
If Not Convert.ToString(DataBinder.Eval(e.Item.DataItem, "ModelName")).Substring(0, 11) = "NAME STRIP:" AndAlso Not nafeco.WebStore.DEK051.GetSubItems(Convert.ToString(DataBinder.Eval(e. Item.DataItem, "ConfigID"))) Is Nothing Then
e.Item.Cells(2).Text = DataBinder.Eval(e.Item.DataItem, "ModelName") & "<br>" & _
"<span style=""font-size: 8pt; color: #6600ff; font-family: Arial; text-decoration: underline"">Click To View SubItems</span>"
With e.Item.Cells(2)
.Attributes("OnClick") = "javascript:window.open('../DEK051/SubItems.aspx?" & "ConfigID=" & Convert.ToString(DataBinder.Eval(e.Item.DataItem, "ConfigID")).Trim & "','SubItems','toolbar=0,menubar=0, status=0,location=0,titlebar=0,scrollbars=0,resizable=1,width=450,heig ht=400,top=0,left=0'); return false;"
.Attributes("OnMouseOver") = "style.cursor='pointer'"
End With
End If

End If

If e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(5).Text = "Total: " & String.Format("{0:c}", OrderTotal)
End If

End Sub

wayneph
01-04-2007, 03:46 PM
Try adding this in the ComputeSum method. I don't see where it's doubling, but this should hopefully clear it up. I think it's trying to DataBind twice, but I'm tired enough I can't find it.
If e.Item.ItemType = ListItemType.Header Then
OrderTotal=0
End If

BTW, How is OrderTotal defined? I don't see it declared anywhere...

easyeman
01-05-2007, 11:29 AM
Try adding this in the ComputeSum method. I don't see where it's doubling, but this should hopefully clear it up. I think it's trying to DataBind twice, but I'm tired enough I can't find it.
If e.Item.ItemType = ListItemType.Header Then
OrderTotal=0
End If

BTW, How is OrderTotal defined? I don't see it declared anywhere...

Thanks and yeah I've been searching for it for a while...I just had enough since I couldn't find anything and that's why I posted. It's just odd that it would do it, and I've not encountered that before...it just seems inconsistent in that it doubles only once when clicking Update, but goes back to normal when clicking Cart, and overall it processes correctly when checking out so it is only the display that is doubling and not what the customer actually pays. I'll try that out to see if it works and hopefully I can get it to stop doubling the total shown. I appreciate the help.

Oh and OrderTotal is declared right above the code I posted in the overall class for ShoppingCart...I didn't post the few bits of code in the top section since it just declares a few variables such as cart, cartID, and OrderTotal, the total declaration is this:

Dim OrderTotal As Decimal

Sorry about that but I did declare it in there. I'll try what you have posted to see if that helps since this is bugging me to no end. I've got another project I'm working on now, and heck I may need help before I'm done haha, so if you see me post again it'll be for that.

easyeman
01-05-2007, 11:41 AM
Hey that did do the trick and I tested it out and it's not doubling the total now, so thanks!

But now I just noticed something else annoying it's doing haha...it's like there's one thing after the other. When you add an item to the cart and add another of the same item, it wants to add another line item for it instead of just having it added to what's there. In other words, it'll show the exact same item twice with quantity of 1 instead of that one item with qty of 2.

The odd thing is too that it's similiar to ordertotal in it wants to copy the thing twice but it won't actually compute it twice. I mean that I added the same item to the cart, and it shows up twice, but when I update quantity on one it does it to the other...it's basically mirroring each other, and when I try to remove the one item, both are removed and gives me an empty cart.

It's just odd but when I do that and then readd the item, it just shows up one like it should...but I dunno it's being weird and I know this can probably cause confusion to the customer since he'll want to order one but if he presses back or tries to add it again, then it can double up on him. I really noticed it happening when I add an item and stay idle to where I have to log in again, then it'll show the cart is empty, but upon reading the item it shows double. Just a weird glitch.

I'll try to figure out what's up with that but if you can can you help me figure out in that code I posted what could cause it to do that? If I need to clarify more I will...shouldn't be hard but prob similiar to the total doubling in that I need to add something in the code.

Here's a picture just to help display it...this is what happened when I added two lights, then tried to add another one...it ended up creating another item...then when I update the quantity on one it automatically does it for the other. This is the result of changing quantity to 2 on one of them. Hope this helps!

http://i5.photobucket.com/albums/y165/easyeman/doubling.jpg

wayneph
01-05-2007, 03:48 PM
you probably don't want to hear this but... Your Add to Cart code isn't on this page.

My suggestion would be to check the cart to see if an item exists when they click the add button. If the Item exists, you can just increment the Quantity instead of going on with the existing Add Code.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum