亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從數據列表更新數據庫表?

如何從數據列表更新數據庫表?

C#
米脂 2022-11-21 16:56:12
我正在建立一個電子商務網站,我使用數據列表來顯示我的產品,當我按下“添加到購物車”按鈕時,它們應該被添加到數據庫表中,但由于某種原因,產品沒有被在數據庫表中更新。此外,單擊第一個產品會出現錯誤,提示“已經有一個打開的閱讀器”,顯示錯誤的行是“int result = comm.ExecuteNonQuery();”using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;using System.Configuration;using System.Globalization;namespace MWM1812{    public partial class productCatalog : System.Web.UI.Page    {        public enum MessageType { Success, Error, Info, Warning };        protected void ShowMessage(string Message, MessageType type)        {            ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), "ShowMessage('" + Message + "','" + type + "');", true);        }        protected void Page_Load(object sender, EventArgs e)        {        }        protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)        {        }        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)        {            if (e.CommandName == "AddToCart")            {                Session["uid"] = 1;                Label lbl = (Label)e.Item.FindControl("PIDLabel");                lblAtC.Text = lbl.Text;                ShowMessage("Product successfully added to Cart", MessageType.Success);                string connStr = ConfigurationManager.ConnectionStrings["MWM1812ConnString"].ConnectionString;                SqlConnection conn = new SqlConnection(connStr);                conn.Open();                string sqlQuery = "SELECT * FROM tblShoppingCart WHERE uid=@uid AND pid=@pid";                SqlCommand comm = new SqlCommand(sqlQuery, conn);                comm.Parameters.AddWithValue("@uid", Session["uid"]);                comm.Parameters.AddWithValue("@pid", lblAtC.Text.Trim());                    }                }            }        }    }}
查看完整描述

1 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

嘗試像這樣更新代碼:


if (e.CommandName == "AddToCart") {

     bool productExists = false;


     Session["uid"] = 1;

     Label lbl = (Label) e.Item.FindControl("PIDLabel");

     lblAtC.Text = lbl.Text;

     ShowMessage("Product successfully added to Cart", MessageType.Success);


     string connStr = ConfigurationManager.ConnectionStrings["MWM1812ConnString"].ConnectionString;

     SqlConnection conn = new SqlConnection(connStr);


     conn.Open();


     string sqlQuery = "SELECT * FROM tblShoppingCart WHERE uid=@uid AND pid=@pid";

     SqlCommand comm = new SqlCommand(sqlQuery, conn);

     comm.Parameters.AddWithValue("@uid", Session["uid"]);

     comm.Parameters.AddWithValue("@pid", lblAtC.Text.Trim());


     using(SqlDataReader reader = comm.ExecuteReader()) {    

        productExists = reader.HasRows;

     }


      if (productExists) {

       string sqlQuery2 = "UPDATE tblShoppingCart SET qty=qty+1 WHERE uid=@uid AND pid=@pid";


       // Add code for adding parameters and executing sqlQuery2 

      } else {




        string sqlQuery1 = "INSERT INTO tblShoppingCart (uid, pid, qty, dtShopped) VALUES (@uid, @pid, @qty, @dtShopped)";

        SqlCommand comma = new SqlCommand(sqlQuery, conn);

        comma.Parameters.AddWithValue("@uid", Session["uid"]);

        comma.Parameters.AddWithValue("@pid", lblAtC.Text);

        comma.Parameters.AddWithValue("@qty", 1);

        //comm.Parameters.AddWithValue("@dtShopped", DateTime.Now.ToLongDateString());

        comma.Parameters.AddWithValue("@dtShopped", DateTime.Now.ToString());


        int result = comma.ExecuteNonQuery();



      }     

    }

注意使用 reader.HasRows 屬性來確定產品是否已經存在。此外,SqlDataReader 被包裝在using塊內以將其處理掉。


查看完整回答
反對 回復 2022-11-21
  • 1 回答
  • 0 關注
  • 102 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號