c# - ASP.NET 未显示任何错误,但无法将数据插入数据库
问题描述
在此之下,我试图将用户选择的购物车项目保存到数据库中,因为我最初通过Request.Query
方法选择了所选择的项目,之后我调用了这些值并形成了一个SaveCartDetail
函数,我在其中执行了插入命令到数据库,asp.net 没有显示错误,但我的表没有变化,我的表的名称是购物车。
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("sno");
dt.Columns.Add("itemname");
dt.Columns.Add("quantity");
dt.Columns.Add("price");
dt.Columns.Add("totalprice");
dt.Columns.Add("image");
if (Request.QueryString["itemname"] != null)
{
if (Session["Buyitems"] == null)
{
dr = dt.NewRow();
SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["online food orderingConnectionString"].ConnectionString);
scon.Open();
String myquery = "select * from food_items where item_name=@items_name";
SqlCommand cmd = new SqlCommand(myquery, scon);
cmd.Parameters.AddWithValue("items_name", Request.QueryString["itemname"].ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dr["sno"] = 1;
if (ds.Tables[0].Rows.Count > 0)
{
dr["itemname"] = ds.Tables[0].Rows[0]["item_name"].ToString();
dr["image"] = ds.Tables[0].Rows[0]["image"].ToString();
dr["price"] = ds.Tables[0].Rows[0]["price"].ToString();
int price = Convert.ToInt16(ds.Tables[0].Rows[0]["price"].ToString());
int quantity = Convert.ToInt16(Request.QueryString["quantity"].ToString());
int totalprice = price * quantity;
dr["quantity"] = Request.QueryString["quantity"];
dr["totalprice"] = totalprice;
SaveCartDetail(ds.Tables[0].Rows[0]["item_name"].ToString(), Request.QueryString["quantity"], ds.Tables[0].Rows[0]["price"].ToString(), totalprice.ToString());
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["buyitems"] = dt;
GridView1.FooterRow.Cells[4].Text = "Total Amount";
GridView1.FooterRow.Cells[5].Text = grandtotal().ToString();
}
}
}
}
private void SaveCartDetail(String itemname, String quantity, String price, String totalprice)
{
String query = "insert into cart(item_name, quantity, price, totalprice, username) values ('" + itemname + "','" + quantity + "','" + price + "','" + totalprice + "','" + Session["username"].ToString() + "')";
SqlConnection scon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["online food orderingConnectionString"].ConnectionString);
scon1.Open();
SqlCommand cmd1 = new SqlCommand(query, scon1);
cmd1.ExecuteNonQuery();
scon1.Close();
Response.Write("Items saved in cart");
}
解决方案
推荐阅读
- android - 如何在 android 的 volley post 请求中永久放置有效的 cookie?
- python - 如何将打印语句的输出也放入用户剪贴板
- java - 如何为我的 Traccar 开源项目下载和连接 MySql 以进行 GPS 跟踪
- android-studio - 有没有办法在没有 vpn 的情况下创建一个新的颤振项目?
- jquery - 在 contenteditable 中交替字体颜色
- python - 使用正则表达式在python中的数据框或列中的大写字母之前添加空格
- sql - 错误 1022:无法写入;表 '#sql-209c_8' SQL 语句中的重复键:
- php - 选项数据未在数据库中更新
- macos - 在本地无法在本地访问的 mac os 上设置 presto 服务器
- apache-beam - 关于 Apache Beam 窗口化策略的建议