ASP.NET - Qry Err - Asked By Jahir on 18-Feb-12 08:45 AM

Err :

Error:System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt) at System.Single.Parse(String s, NumberStyles style, NumberFormatInfo info) at System.Single.Parse(String s) at _Default.GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) in d:\running\Default.aspx.cs:line 138

my coding is:

cmd = new SqlCommand("select SUM(ISNULL(amount,0)) AS total from amt", con);

dr = cmd.ExecuteReader();

dr.Read();


if (dr.HasRows)

{

if (dr[0] != DBNull.Value)

{

amount =

Convert.ToSingle(dr[0]);

}

else

amount = 0;

float f = amount + float.Parse(TextBox1.Text); //error ?

dr.Close();




cmd =

new SqlCommand("update amt set amount='" + txamount.Text + "',total='" + f + "' where id ='" + lbid.Text + "'", con);

cmd.ExecuteNonQuery();

GridView1.EditIndex = -1;

Response.Write("Updated Successfully");

bindgrid();


}

else

{

dr.Close();

}


Chintan Vaghela replied to Jahir on 18-Feb-12 09:01 AM
Hello,

When convert any field decimal or float then this field always be numberic

for error float.Parse(TextBox1.Text);  this you need to code as following way
decimal amt = 0;
try
{
if(TextBox1.Text != "")
{
  amt = Convert.ToDecimal(TextBox1.Text);
}
 }
catch
{
amt = 0;
}
Deimal f = amount + amt;

Hope this helpful