Looping Problem

VonEhle

Active member
Joined
Feb 20, 2006
Messages
26
Programming Experience
Beginner
I am attaching a school project I am working on. The problem I'm having is when I add an item to the cart, it always doubles the quantity. I have figured out that the problem is that the program is looping, causing it to do several steps twice. I cannot figure out why it is looping. Obviously there is some kind of logic error, but I'll be darned if I can find out where. I would appreciate anyone's input on why this is happening.

Thanks a ton.
 
Double Loop

Here's the code for the part that is double looping.
VB.NET:
Sub AddToCart(ByVal s As Object, ByVal e As EventArgs) Handles btnAdd.Click
    objDT = Session("cart")
    Dim Product As String
    Product = ddlProducts.SelectedItem.Text
    Dim blnMatch As Boolean = False
    For Each objDR In objDT.Rows
        If objDR("Product") = Product Then
            objDR("Quantity") += txtQuantity.Text
            blnMatch = True
            Exit For
        End If
    Next
    If Not blnMatch Then
        objDR = objDT.NewRow
        objDR("Quantity") = Int32.Parse(txtQuantity.Text)
        objDR("Product") = ddlProducts.SelectedItem.Text
        objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
        objDT.Rows.Add(objDR)
    End If
 
    Session("Cart") = objDT
    DG.DataSource = objDT
    DG.DataBind()
    lblTotal.Text = "$" & GetItemTotal()
 
End Sub

Function GetItemTotal() As Decimal
    Dim decRunningTotal As Decimal
    For intCounter = 0 To objDT.Rows.Count - 1
        objDR = objDT.Rows(intCounter)
        decRunningTotal += (objDR("Cost") * objDR("Quantity"))
        MsgBox("cost " & objDR("cost") & "Qty " & objDR("Quantity"))
    Next
    Return decRunningTotal
End Function
 
Back
Top