question

akhterhussain-3167 avatar image
0 Votes"
akhterhussain-3167 asked Viorel-1 edited

Input string was not in a correct format.

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();
         }
dotnet-csharpdotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

If the value is decimal, then try decimal price = Convert.ToDecimal(txtprice.Text) and maybe adjust other things.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.