1

How can I convert the value of ASP.NET ((TextBox)Gridview1Row.FindControl("Amount")).text of its value to Float.

foreach (GridViewRow Gridview1Row in Gridview1.Rows)
{            
DataRow OneDataRow;

OneDataRow["Amount"]= ((TextBox)Gridview1Row.FindControl("Amount")).text  //Need to convert to float
}

I'm having incorrect datatype format. I tried float.Parse and (float) and still having an error.

Thanks in advance

Ivan Gechev
  • 706
  • 3
  • 9
  • 21
  • Did you debug your code and check what are you getting from `((TextBox)Gridview1Row.FindControl("Amount")).text`? – Chetan Oct 04 '22 at 02:50
  • I debugged it and entered numeric value. During debug mode the value on the textbox control inside the Gridview was numeric. – gwiyaoming yaoming Oct 04 '22 at 03:06
  • I also tried this TextBox txtAmount = ((TextBox)rRow.FindControl("Amount")); Amount = float.Parse(txtAmount.Text); OneDataRow["Amount"] = Amount; but still getting an error of "Input string was not in a correct format." – gwiyaoming yaoming Oct 04 '22 at 03:07
  • @gwiyaomingyaoming string amountStr = ((TextBox)rRow.FindControl("Amount")).Text; Amount = float.Parse(amountStr); OneDataRow["Amount"] = Amount; try this and make sure the input you get is numeric – nsnd64 Oct 04 '22 at 03:31
  • Did you check the value of `txtAmount.Text` while debugging? is it the same value you entered in the UI? – Chetan Oct 04 '22 at 03:31
  • For the record this following codes work: TextBox txtAmount = ((TextBox)rRow.FindControl("Amount")); Amount = float.Parse(txtAmount.Text); OneDataRow["Amount"] = Amount; string amountStr = ((TextBox)rRow.FindControl("Amount")).Text; Amount = float.Parse(amountStr); OneDataRow["Amount"] = Amount; Found out the issue was the existing Null value on the Amount Column of the table. I just applied some condition on the null value of the Float datatype to cleanse it. Thank you guys! Cheers – gwiyaoming yaoming Oct 04 '22 at 06:13

0 Answers0