I am trying to store my gridview into a database but I can't seem to do it. How should I solve it?
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();
}
I get this error:
"Input string was not in a correct format"
from the following lines and I have changed the codes many times but it still does not work.
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));