Input string was not in a correct format.

Analyst_SQL 3,531 Reputation points
2020-11-25T12:43:12.727+00:00

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();
        }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,246 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
0 comments No comments
{count} votes

0 additional answers

Sort by: Most helpful