-1

我刚刚问了一个问题,并相应地修改了我的代码。我写我的页面加载的方式有问题。我的目标是仅第一次初始化数组,然后继续递增它。你能帮我吗?这是我使用的代码:

Imports AjaxControlToolkit
Imports System.Data.SqlClient
Imports System.Configuration



Partial Class Shtick

    Inherits System.Web.UI.Page
    Dim conn As SqlConnection
    Dim comm As SqlCommand
    Dim reader As SqlDataReader
    Dim purimConnection As String = ConfigurationManager.ConnectionStrings("Purim").ConnectionString
    Dim ItemSelect As New ArrayList()



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load




        If ItemSelect.Count >  0 Then
            ItemSelect = New ArrayList()
            Session("itemInCart") = ItemSelect
        End If

        If Not IsPostBack Then
            FillShtickList()

        End If

    End Sub

    Protected Sub FillShtickList()

        Dim conn As SqlConnection
        Dim comm As SqlCommand
        Dim reader As SqlDataReader
        Dim purimConnection As String = ConfigurationManager.ConnectionStrings("Purim").ConnectionString
        conn = New SqlConnection(purimConnection)
        comm = New SqlCommand("SELECT RTRIM(ProductPrice) AS Price, ProductName, ProductImage, ProductID, ProductDescription FROM Products", conn)

        Try
            conn.Open()
            reader = comm.ExecuteReader()
            ShtickDataList.DataSource = reader
            ShtickDataList.DataBind()
            reader.Close()

        Finally
            conn.Close()

        End Try
    End Sub


    'Protected Sub ShtickDataList_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ShtickDataList.ItemCreated
    '    'If e.Item.ItemType = ListItemType.Item Then
    '    '    Dim pce As ModalPopupExtender = e.Item.FindControl("PopupControlExtender1")
    '    '    Dim behaviorID As String
    '    '    behaviorID = "pce_" & e.Item.DataItemIndex
    '    '    pce.BehaviorID = behaviorID
    '    '    Dim img As Image = e.Item.FindControl("PI")

    'item select = which item was selected

    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 = (ItemSelected.Count - 1)

        'Next

        Try
            conn.Open()
            reader = comm.ExecuteReader()
            ViewCartlink.Text = "View Cart: (" & ItemSelected.Count & ")"



        Finally
            conn.Close()
        End Try
    End Sub

End Class


 End Try
    End Sub

End Class


 = CType(Session("itemInCart"), ArrayList)
            'al.Add(SS)
            'Session.Add("itemInCart", al)

            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

End Class


M(ProductPrice) AS Price, ProductID, ProductName FROM Products WHERE ProductID = @ProductID", conn)
        comm = New SqlCommand("Insert into orders (UserID, ProductID, quantity) values (1, @ProductID, @Quantity)", conn)
        comm.Parameters.Add("@ProductID", Data.SqlDbType.Int)
        comm.Parameters("@ProductID").Value = item
        comm.Parameters.Add("@Quantity", Data.SqlDbType.Int)
        comm.Parameters("@Quantity").Value = qty

        'Dim i As Integer
        'For i = 0 To ItemSelect.Length - 1                                           
        '    comm.Parameters.Add("@ProductID", Data.SqlDbType.Int)
        '    comm.Parameters("@ProductID").Value = ItemSelect(i)
        'Next
        Try
            conn.Open()
            comm.ExecuteNonQuery()
            'reader = comm.ExecuteReader()
            'ShoppingList.DataSource = reader
            'ShoppingList.DataBind()
            'reader.Close()
        Finally
            conn.Close()
        End Try
    End Sub
4

1 回答 1

0

在代码中:

If ItemSelect.Count >  0 Then
    ItemSelect = New ArrayList()
    Session("itemInCart") = ItemSelect
End If

你有一个大于你应该有一个小于的地方。数组列表仅在数组中有项目时才初始化。所以你只有在初始化后才初始化数组。你需要的是:

If ItemSelect.Count >  0 Then
    ItemSelect = New ArrayList()
    Session("itemInCart") = ItemSelect
End If
于 2013-03-08T19:23:10.203 回答