我在gridview上执行update命令时收到错误“FormatException未被用户代码处理”? [英] i am getting an error "FormatException was unhandled by user code"while executing update command on gridview?
本文介绍了我在gridview上执行update命令时收到错误“FormatException未被用户代码处理”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我在gridview rowupdating事件中使用的ccode:
this is the ccode i m using under gridview rowupdating event:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
int autoid = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("Label12")).Text);// error is in this line
string pcode = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblp")).Text;
string fyyear = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblf")).Text;
string date = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;
string salary = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4")).Text;
string ta = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5")).Text;
string contigency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6")).Text;
string nrc= ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7")).Text;
string institcharges = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox8")).Text;
string others = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox9")).Text;
string str = "update monthly set date='" + date + "',salary='" + salary + "',ta='" + ta + "',contigency='" + contigency+ "',nrc='" + nrc+ "',institcharges='" + institcharges+"' where autoid=" + autoid;
SqlCommand cmd = new SqlCommand(str, con);
cmd.Connection = con;
// con.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
GridView1.EditIndex = -1;
grid_show();
}
请帮我这个?
please help me with this?
推荐答案
int autoid = Convert.ToInt32(((Label)GridView1.Rows [e.RowIndex] .FindControl(Label12))。Text); //错误在这一行
Label Label12的文本不是一个整数值。 FormatException [ ^ ]。 />
您可以使用 Int32.TryParse方法 [ ^ ]而不是。这将尝试解析你的字符串,如果它是一个正确的整数,它将返回true,并且该值将在out变量中返回,然后可以用作参数。
在该行上放置一个调试器,检查标签文本,你会很清楚。
希望这会有所帮助!
int autoid = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("Label12")).Text);// error is in this line
The text of your Label Label12 isn''t an interger value. FormatException[^] is thrown when the format of an argument does not meet the parameter specifications of the invoked method.
You could use Int32.TryParse Method[^] instead. This will try to parse your string and if it''s a correct integer, it will return true and the value will be returned in an out variable which then can be used as a parameter.
Put a debugger on that line, check the label text and things will be very clear to you.
Hope this helps!
这篇关于我在gridview上执行update命令时收到错误“FormatException未被用户代码处理”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文