ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
When i am inserting Decimal value from textbox to gridview asp.net Webform, then below error is coming ,
Input string was not in a correct format.
Below line
int price = Convert.ToInt16(txtprice.Text);
My Code for inserting data into gridview is below.
protected void B12_Click(object sender, EventArgs e)
{
con.Open();
SqlDataAdapter adpbp1 = new SqlDataAdapter("select * from tbl_bottles where B_ID='1002'", con);
DataSet dsbp1 = new DataSet();
adpbp1.Fill(dsbp1);
DDLitem.DataSource = dsbp1.Tables[0];
DDLitem.DataTextField = "B_Name";
DDLitem.DataValueField = "B_ID";
DDLitem.DataBind();
con.Close();
if (DDLitem.SelectedValue == "1002")
{
if (txtratem.Text != "")
{
txtprice.Text = txtratem.Text;
}
else
{
itemPrice12();
}
}
// DropDownList1.Items.Insert(0, new ListItem("Select Item Name", ""));
bool ifExist = false;
int codeitem = Convert.ToInt32(DDLitem.SelectedItem.Value.ToString());
string item = DDLitem.SelectedItem.Text;
int quantity = Convert.ToInt16(txtqty.Text);
int price = Convert.ToInt16(txtprice.Text);
DataTable dt;
if (ViewState["dt"] == null)
{
dt = new DataTable();
dt.Columns.Add("B_ID");
dt.Columns.Add("B_Name");
dt.Columns.Add("QTY");
dt.Columns.Add("Rate");
dt.Rows.Add(codeitem, item, quantity, price);
ViewState["dt"] = dt;
}
else
{
dt = ViewState["dt"] as DataTable;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["B_Name"].ToString().ToLower() == item.ToLower())
{
dt.Rows[i]["QTY"] = Convert.ToInt16(dt.Rows[i]["QTY"]) + quantity;
dt.Rows[i]["Rate"] = Convert.ToInt16(dt.Rows[i]["Rate"]) + price;
ifExist = true;
break;
}
}
if (!ifExist)
{
dt.Rows.Add(codeitem, item, quantity, price);
}
ViewState["dt"] = dt;
}
GVFood.DataSource = dt;
GVFood.DataBind();
}
If the value is decimal, then try decimal price = Convert.ToDecimal(txtprice.Text) and maybe adjust other things.