mysql - 如何将新项目插入 session 而不覆盖以前的值 ASP/VB.NET

标签 mysql asp.net vb.net session

我正在使用 ASP 和 VB.net 制作一个网站(我有一个 mysql 数据库,其中存储了所有产品),我需要创建一个添加到购物车按钮以便稍后将其显示在购物车中。

为此,我决定将数据库中的商品 ID 存储到 session 变量中,然后在购物车页面上检索它以显示有关产品的其他信息。但每次用户单击按钮将商品添加到购物车时,我都需要向 session 数组添加一个新变量。但我不知道如何在每次单击时向数组添加变量。

    Private _cmd As MySqlCommand
    Private _adapter As MySqlDataAdapter
    Dim myCookie As HttpCookie = New HttpCookie("Cart")
    Dim item As Integer
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ViewProduct()
    End Sub

    Private Sub ViewProduct()
        Dim Cart(10) As String
        Dim QueryStr As String
        _conn = New MySqlConnection
        _conn.ConnectionString = ConfigurationManager.ConnectionStrings("ProductConn").ConnectionString
        Dim _reader As MySqlDataReader
        _conn.Open()

        QueryStr = "SELECT * FROM products.items "
        _cmd = New MySqlCommand(QueryStr, _conn)
        _reader = _cmd.ExecuteReader()

        For index As Integer = 0 To 48 Step 1
            _reader.Read()
            If Request.RawUrl = "/ZTY_Fashion/Scripts/Viewproduct.aspx?id=" + _reader("productID").ToString Then
                ImageButton1.ImageUrl = _reader("productImg").ToString
                name.Text = _reader("productName").ToString
                product.Text = _reader("productDisc").ToString
                price.Text = _reader("productPrice").ToString
                addtocart.Text = "Buy Now"
                quantity.Text = "Quantity in stock" + " " + _reader("Instock").ToString
                quantitytxt.Text = "Quantity"
                similar.Text = "Similar Items in stock"
                ID.Text = _reader("productID").ToString

            End If
        Next index

        _reader.Close()

        Dim rnd = New Random()
        Dim nextValue = rnd.Next(48) / 1

        QueryStr = "SELECT * FROM products.items WHERE productID='" & nextValue & "'"
        _cmd = New MySqlCommand(QueryStr, _conn)
        _reader = _cmd.ExecuteReader()

        For i As Integer = 0 To 48 Step 1
            _reader.Read()
            Select Case i
                Case 0
                    imgdisplay.ImageUrl = _reader("productImg").ToString
            End Select
        Next i

        _reader.Close()
        _conn.Close()
    End Sub

    Protected Sub addtocart_Click(sender As Object, e As EventArgs) Handles Button20.Click

    //This is where i would like to add the code
    End Sub
End Class

最佳答案

您可以在 session 中存储您自己的自定义对象列表,然后只需将这些对象的集合存储在您的 session 中,例如 List<CustomerCartItem> 。将其转换为 VB.net,就像在 C# 中一样

创建一个类

public class CustomerCartItem
{
   // add what you need here, if you dont need quantity or name, just remove 
   // both, and only take Product ID
   public int ProductID {get;set;}
   public int Quantity {get;set;}
   public int Name {get;set;}
}

当您要在购物车中添加新产品时

if(Session["customerCartItem"] != null){
   List<CustomerCartItem> items = (List<CustomerCartItem>)Session["customerCartItem"];
   items.Add(new CustomerCartItem() { ProductID = 1, Quantity = 2, Name = 'Prod1' }) 
   Session["customerCartItem"] =   items ;
}
else
{
List<CustomerCartItem> items = new List<CustomerCartItem>();
items.Add(new CustomerCartItem() { ProductID = 1, Quantity = 2, Name = 'Prod2' })
Session["customerCartItem"] =   items ;
}

关于mysql - 如何将新项目插入 session 而不覆盖以前的值 ASP/VB.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28614894/

相关文章:

asp.net 无法在 dot net 4.0 中运行

javascript - 当浏览器缩放更改时如何使用javascript检索tr高度?

php - 我这里的代码太多,想简短一点

mysql - 使用 phpMyAdmin 将带有部分数据的制表符分隔的 csv 文件导入到 mysql 表中

MySQL - 修剪 VARCHAR 值?

c# - 使用带有谓词的 linq 无法识别表达式节点 ArrayIndex

asp.net - 从 Internet 打开 excel 文件会打开一个空白的 excel 窗口

.net - 为什么 HttpWebRequest 和 WebBrowser 获得不同的 HTML 源代码?

vb.net - 为什么我无法在 vb.net Visual Studio 2010 中声明应用程序事件?

php - 我想使用带有 AJAX 的文本框搜索