gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:58:31 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com