我正在尝试将gridview存储到数据库中,但似乎无法做到。我该怎么解决?
foreach (GridViewRow row in gv_CartView.Rows)
{
int quantity = Convert.ToInt32(row.Cells[2].Text);
Session["Quantity"] = quantity;
decimal TotalPrice = Convert.ToDecimal(row.Cells[4].Text);
Session["TotalPrice"] = TotalPrice;
string queryStr = "INSERT into Cart (prod_Id,prod_Name,qty,prod_Price,total_Amt)"
+ "values (@ItemID,@Product_Name,@Quantity,@Product_Price,@TotalPrice)";
SqlConnection conn = new SqlConnection(_connStr);
SqlCommand cmd = new SqlCommand(queryStr, conn);
cmd.Parameters.AddWithValue("@ItemID", row.Cells[0].Text);
cmd.Parameters.AddWithValue("@Product_Name", row.Cells[1].Text);
cmd.Parameters.AddWithValue("@Quantity", Convert.ToInt32(row.Cells[2].Text));
cmd.Parameters.AddWithValue("@Product_Price", Convert.ToDecimal(row.Cells[3].Text));
cmd.Parameters.AddWithValue("@TotalPrice", Convert.ToDecimal(row.Cells[4].Text));
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
我收到此错误:
“输入字符串的格式不正确”
从以下几行开始,我已经多次更改了代码,但仍然无法正常工作。
cmd.Parameters.AddWithValue("@Quantity", Convert.ToInt32(row.Cells[2].Text));
cmd.Parameters.AddWithValue("@Product_Price", Convert.ToDecimal(row.Cells[3].Text));
cmd.Parameters.AddWithValue("@TotalPrice", Convert.ToDecimal(row.Cells[4].Text));