2013-02-09 64 views
0

这是我更新事件代码:在GridView控件更新编码我收到以下错误?

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    if (con.State == ConnectionState.Closed) 
    { 
     con.Open(); 
    } 

    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; 
    int Label11 =Convert.ToInt32(((Label)row.FindControl("Label11")).Text);// this is the line m getting error in 
    int Label12 = Convert.ToInt32(((Label)row.FindControl("Label12")).Text); 
    int Label13 = Convert.ToInt32(((Label)row.FindControl("Label13")).Text); 
    TextBox TextBox4 = (TextBox)row.FindControl("TextBox4"); 
    TextBox TextBox5 = (TextBox)row.FindControl("TextBox5"); 
    TextBox TextBox6 = (TextBox)row.FindControl("TextBox6"); 
    TextBox TextBox7 = (TextBox)row.FindControl("TextBox7"); 
    TextBox TextBox8 = (TextBox)row.FindControl("TextBox8"); 
    TextBox TextBox9 = (TextBox)row.FindControl("TextBox9"); 
    TextBox TextBox10 = (TextBox)row.FindControl("TextBox10"); 
    GridView1.EditIndex = -1; 
    SqlCommand cmd = new SqlCommand("update monthly set date='" + TextBox4.Text + "',salary='" + TextBox5.Text + "',ta='" + TextBox6.Text + "',contigency='" + TextBox7.Text + "',nrc='" + TextBox8.Text + "',institcharges='" + TextBox9.Text + "',others='" + TextBox10.Text + "' where autoid='" + Label12 + "'", con); 
    cmd.ExecuteNonQuery(); 
    cmd.Dispose(); 
    con.Close(); 
    grid_show(); 

I M得到的错误是出现FormatException了未处理BU用户代码 输入字符串的不正确的格式。

+0

首先是容易被SQL注入的目标。使用实体框架之类的ORM框架或使用参数化查询。这是一场灾难。用户'TryParse'方法原始数据类型,以避免异常,并从那里准备参数。 – 2013-02-09 09:30:52

+0

@JigarPatel:这可能只是值得写这两个点作为答案... – 2013-02-09 09:38:18

+0

jigar嘿米新的这种方法可以告诉我如何使用这个我一直使用convert.toin32进行转换。 – a2ulthakur 2013-02-09 09:39:00

回答

1

使用的TryParse方法适用于基本数据类型。解释SQL注入和ORM框架超出了这个答案的范围。

http://social.msdn.microsoft.com/Search/en-US?query=TryParse&ac=8的所有代码

void Main() 
{ 
    //TryParse function signature 
    //bool TryParse(String, Int32) 
    //Above method is for Integer. There are similar methods for all primitive data types 

    //TryParse takes String value as an input and parameter as reference 

    //Integer example 
    int result; 
    bool success = int.TryParse("10", out result); 
    if(success) Console.WriteLine("Good value {0}", result); 


    //DateTime example 
    DateTime dtResult; 
    success = DateTime.TryParse("01/10/2013", out dtResult); 
    if(success) Console.WriteLine("Good date {0}", dtResult); 
} 
+0

感谢乌拉圭回合的帮助.. !!无论如何,如果有谁可以帮助...将是伟大的! – a2ulthakur 2013-02-09 13:22:09

相关问题