ASP.NET - Update Err in Grand total

Asked By Jahir on 10-Feb-12 09:33 AM
Ihave error on updating i given my error here kindly findout and help me

This is my 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 creditadd.GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) in d:\tour\creditadd.aspx.cs:line 233 .style2 { width: 18%; height: 39px; } .style3 { width: 1764px; } .style1 { width: 226%; height: 26px; } .ctl00_Menu1_0 { background-color:white;visibility:hidden;display:none;position:absolute;left:0px;top:0px; } .ctl00_Menu1_1 { text-decoration:none; } .ctl00_Menu1_2 { background-color:#669900;height:35px;width:1256px; }

update codings:

else
if (e.CommandName == "upd")

{


LinkButton lkbupd = (LinkButton)e.CommandSource as LinkButton;

GridViewRow row = lkbupd.NamingContainer as GridViewRow;

Label lbid = (Label)row.FindControl("lblid");

TextBox txusrid = (TextBox)row.FindControl("txtusrid");

TextBox txusrname = (TextBox)row.FindControl("txtusrname");

TextBox txservices = (TextBox)row.FindControl("txtservices");

TextBox txcredit = (TextBox)row.FindControl("txtcredit");

TextBox txtotal = (TextBox)row.FindControl("txttotal");

cmd =

new SqlCommand("select sum(credit) AS total from creditadd where cid<=cid and credit='" + txcredit.Text + "' and total='" + txtotal.Text + "'", con);

dr = cmd.ExecuteReader();

dr.Read();

if(dr.HasRows)

{

total=

float.Parse(dr[0].ToString()); //error this line: Input string was not in a correct format.

float f=total+float.Parse(txcredit.Text);

dr.Close();


cmd =

new SqlCommand("update creditadd set credit='" + txcredit.Text + "' where cid ='" + lbid.Text + "'", con);

cmd.ExecuteNonQuery();

GridView1.EditIndex = -1;

Response.Write("<script>alert('Updated Successfully');</script>");

}


else

{

dr.Close();

}

cmd = new SqlCommand("select cid from creditadd where userid='" + txusrid.Text + "' and username='" + txusrname.Text + "'", con);

adap =

new SqlDataAdapter(cmd);

ds = new DataSet();

adap.Fill(ds);


for(int k=0;k<ds.Tables[0].Rows.Count;k++)

{

int c =int.Parse(ds.Tables[0].Rows[k]["cid"].ToString());

cmd = new SqlCommand("select total from creditadd where cid='"+c.ToString()+"'",con);

dr = cmd.ExecuteReader();

dr.Read();

if (dr.HasRows)

{

total =

float.Parse(dr[0].ToString());

float f = total + float.Parse(txcredit.Text);

dr.Close();

cmd =

new SqlCommand("update creditadd set credit='" + txcredit.Text + "' where cid ='" + lbid.Text + "'", con);

cmd.ExecuteNonQuery();

}


else

{

dr.Close();

}

}

kalpana aparnathi replied to Jahir on 10-Feb-12 01:06 PM
hi,

I suggest you you TryParse to ensure your code will not throw an exception if it fails converting to a number and you can define a default value.

hope will works!!!

Thanks,