首页 > 解决方案 > 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");
}

标签: c#sqlasp.net

解决方案


推荐阅读