Input string was not in a correct format error while saving data from GridView to database [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Input string was not in a correct format error while saving data from GridView to database [duplicate]



This question already has an answer here:



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.



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





The error states that one of the values in your cell is not convertible to integer or decimal.
– Reniuz
28 mins ago





Don't use Convert, it's just a call to int.Parse or decimal.Parse that doesn't allow you to specify additional parameters like CultureInfo.
– Panagiotis Kanavos
10 mins ago


Convert


int.Parse


decimal.Parse





The error comes from the attempt to parse the cell text. Most likely the user entered a decimal value with a separator used in one locale while the web app runs on a different locale. Eg, entered 1,5 when the web app is configured for en-US.
– Panagiotis Kanavos
9 mins ago




1,5


en-US




Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived