数据table + session = 购物车

Data table + session = Shopping cart

GM大家

我在实施购物车时发现了一些问题

查询有效,但不在会话中存储信息。

我会修复查询还是问题出在会话上?

这里是代码:

 Dim constr As String = ConfigurationManager.ConnectionStrings("!aCommerce-ConnectionString!").ConnectionString

    ' Query SQL 
    Using cmd As New SqlCommand("SELECT Id,NomeProdotto, PrezzoProdotto, Quantità FROM aProdotti WHERE ID='" + Request.QueryString("ID").ToString + "' OR ID='" + Request.QueryString("ID").ToString + "'")

            Using sda As New SqlDataAdapter(cmd)
                Dim dt As New DataTable()

                Session("dt") = dt

                sda.Fill(dt)

                GridView1.DataSource = dt
                GridView1.DataBind()


            End Using
        End Using
    End Using

将您的代码更改为以下

  Dim constr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

        Dim con As New SqlConnection()
        con.ConnectionString = constr
        con.Open()
        ' Query SQL 
        Using cmd As New SqlCommand("SELECT Id,PromoCode as NomeProdotto,PromoCodeMessage as  PrezzoProdotto, PromoCodeLimit as Quantità FROM EventPromocodetbl WHERE ID=" & Request.QueryString("ID").ToString & "", con)
            Using sda As New SqlDataAdapter(cmd)
                Dim dt As New DataTable()
                sda.Fill(dt)
                Dim SessionDt As New DataTable()
                SessionDt = Session("dt")

                If Not SessionDt Is Nothing Then

                    If (dt.Rows.Count > 0) Then
                        Dim dr As DataRow
                        dr = SessionDt.NewRow()
                        dr("Id") = dt.Rows(0)("Id").ToString()
                        dr("NomeProdotto") = dt.Rows(0)("NomeProdotto").ToString
                        dr("PrezzoProdotto") = dt.Rows(0)("PrezzoProdotto").ToString
                        dr("Quantità") = dt.Rows(0)("Quantità").ToString

                        SessionDt.Rows.Add(dr)
                    End If

                    Session("dt") = SessionDt
                    GridView1.DataSource = SessionDt
                    GridView1.DataBind()
                Else
                    Session("dt") = dt
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                End If



            End Using
        End Using

问题是您将空白 DatatTable 分配给会话,然后您使用有问题的 DataAdapter 填充方法填充该 DataTable。