我一直試圖在c#asp.net和SQL服務器中創建購物車沒有成功。如何在購物車中使用會話變量
我能夠從數據庫檢索產品併爲每個產品創建詳細視圖。
現在我需要創建按鈕「添加到購物車」,我知道我必須創建一個數組的會話變量來保存產品信息,然後顯示在購物車中,但我不知道如何。
這是我在productsDetail.cs代碼:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection connexionBD = new SqlConnection(this.data_un_jouet.ConnectionString);
SqlCommand commandeJouets = null;
SqlDataReader lecteurJouets = null;
try
{
connexionBD.Open();
String id = Request.QueryString["no"];
Session["product"] = id;
string myQuery = data_un_jouet.SelectCommand;
commandeJouets = new SqlCommand(myQuery, connexionBD);
commandeJouets.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(id);
lecteurJouets = commandeJouets.ExecuteReader();
TableRow uneLigne;
TableCell uneCellule;
while (lecteurJouets.Read())
{
uneLigne = new TableRow();
string myID = Convert.ToString(lecteurJouets["id"]);
uneCellule = new TableCell();
uneCellule.Text = Convert.ToString(lecteurJouets["name"]);
uneLigne.Cells.Add(uneCellule);
uneCellule = new TableCell();
uneCellule.Text = "<img src=" + lecteurJouets["image"].ToString() + "/>";
uneLigne.Cells.Add(uneCellule);
uneCellule = new TableCell();
uneCellule.Text = Convert.ToString(lecteurJouets["description"]);
uneLigne.Cells.Add(uneCellule);
uneCellule = new TableCell();
uneCellule.Text = Convert.ToString(lecteurJouets["price"]);
uneLigne.Cells.Add(uneCellule);
uneCellule = new TableCell();
uneCellule.Text = "<a href=\"cart.aspx?no=" + myID + "\" />Add to cart</a>";
uneLigne.Cells.Add(uneCellule);
this.un_jouet.Rows.Add(uneLigne);
}
lecteurJouets.Close();
commandeJouets.Dispose();
}
catch (Exception ex)
{
msgErreur.Text = "Erreur de connexion ! " + ex.Message;
}
finally
{
connexionBD.Close();
}
我知道的大部分代碼是法語,我試着翻譯了一些。
此代碼工作得很好,問題是我不知道點擊「添加到購物車按鈕」時該怎麼辦。
我創建了一個會話變量,但它只包含產品ID。
非常感謝您的幫助,
非常感謝您的幫助@System Down 我已經在details.cs的頁面加載聲明瞭產品列表 運行程序時出現此錯誤:「對象引用未設置爲對象的實例「。它來自我將我的ID添加到列表中的行:「products.Add(myID);」 我已經添加了條件來檢查會話是否存在於cart.cs中我猜我需要循環列表來檢索產品,但我不知道如何,我知道這是很多要問,但任何幫助對我來說是美好的。 – Xavieres