-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

あなたはあなたがより少ないべきであるところより多くを持っています。arraylistは、配列に項目がある場合にのみ初期化されます。したがって、配列は初期化された後にのみ初期化されます。必要なのは:

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