0

I'm trying to increment my variable on a button click. It increments only once. It seems as though it's getting lost when it reloads the page.

I'm using the following code:

Dim ItemSelect As New ArrayList()
Dim Quantities As New ArrayList()
Dim itemQtyOrdered As Integer

Public Sub ShtickDataList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ShtickDataList.ItemCommand

    If e.CommandName = "ViewCart" Then
        Response.Redirect("~/ShoppingCart.aspx")
    End If

    If e.CommandName = "addToCart" Then
        Dim itemQuantity As DropDownList = e.Item.FindControl("QuantityDropDown")
        itemQtyOrdered = itemQuantity.SelectedValue
        ItemSelect.Add(e.CommandArgument)
        Quantities.Add(itemQtyOrdered)

        Session("itemInCart") = ItemSelect
        Session("quantities") = Quantities

        viewInvoice()
    End If

End Sub

Protected Sub viewInvoice()

    Dim itemSelected As ArrayList = DirectCast(Session("itemInCart"), ArrayList)
    Dim QuantityofItem As ArrayList = DirectCast(Session("quantities"), ArrayList)
    Dim conn As SqlConnection
    Dim comm As SqlCommand
    Dim reader As SqlDataReader
    Dim purimConnection2 As String = ConfigurationManager.ConnectionStrings("Purim").ConnectionString
    conn = New SqlConnection(purimConnection2)

    comm = New SqlCommand("SELECT ProductName FROM Products WHERE ProductID = @ProductID", conn)

    Dim i As Integer
    For i = 0 To ItemSelect.Count - 1
        comm.Parameters.Add("@ProductID", Data.SqlDbType.Int)
        comm.Parameters("@ProductID").Value = ItemSelect(i)
    Next

    Try
        conn.Open()
        reader = comm.ExecuteReader()
        ViewCartlink.Text = "View Cart: (" & ItemSelect.Count & ")"
    Finally
        conn.Close()
    End Try
End Sub
5
  • I do not see any variable being incremented in the code that you provided, nor is there a button click. Commented Feb 28, 2013 at 16:30
  • Surely if you are 'reloading' the page it would reset your variables? Commented Feb 28, 2013 at 16:30
  • what i need is to add items to my array an d then that i loops through the array. how do I avoid reloading the page each time? Commented Feb 28, 2013 at 16:32
  • Can you please point out the variable you are expecting to increment so others can follow your logic? Commented Feb 28, 2013 at 16:33
  • Dim i As Integer For i = 0 To ItemSelect.Count - 1 comm.Parameters.Add("@ProductID", Data.SqlDbType.Int) comm.Parameters("@ProductID").Value = ItemSelect(i) Next Commented Feb 28, 2013 at 16:34

1 Answer 1

1

Ah, you may be referring to ItemSelect and Quantities lists. You need to look for them in Session and only create them if they are not in the Session. I am rusty on VB.NET, so this is C# version. In Page_Load:

ItemSelect = (ArrayList)Session["itemInCart"];
if (ItemSelect == null)
{
  ItemSelect = new ArrayList();
  Session["itemInCart"] = ItemSelect;
}

and the same for Quantities.

Also, your loop in viewInvoice method is wrong. For more than one item in ItemSelect list you are adding multiple parameters with the same name. You probably only wanted to do it once with

comm.Parameters("@ProductID").Value = ItemSelect(ItemSelect.Count - 1)
Sign up to request clarification or add additional context in comments.

6 Comments

I get it, its overiding the first one. So how do I fix that?
thanks. Im going to try this but how do I get arount the perameter problem?
Your sql has only one parameter in it. Therefore you need to add only one parameter to you command. In any event, why are you doing it if you are not using the reader?
when i declare the array only if it hasnt been done yet like you said then the variables are not global
I have not shown any declaration, only assignment. I do not know the proper VB.NET syntax for declaring a class member with value being null (Nothing).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.