Click here to Skip to main content
15,879,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is my update event code :

C#
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();



the error i m getting is FormatException was unhandled bu user code
Input string was not in a correct format.
Posted
Comments
Nandakishore G N 9-Feb-13 6:01am    
did you check using debugging line whether there is some value in the label11..?

HI,

Check whether there is proper data in your levels that ca be convertable to int32 or not. And convert all the textbox values to proper format before using in the sql query.

Thanks
 
Share this answer
 
Comments
a2ulthakur 9-Feb-13 4:33am    
arnav : in my 1st label12 is autoincrement int type
label 13 is varchar
label 14 is also varchar

i was using this query before but it didnt work :
int autoid = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("Label12")).Text);
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();
[no name] 9-Feb-13 4:43am    
Try changing your code to the following code.

SqlCommand cmd = new SqlCommand("update monthly set date='" + TextBox4.Text + "',salary='" + TextBox5.Text + "',ta='" + Convert.ToDateTime(TextBox6.Text) + "',contigency='" + TextBox7.Text + "',nrc='" + TextBox8.Text + "',institcharges='" + TextBox9.Text + "',others='" + TextBox10.Text + "' where autoid='" + Label12 + "'", con);

like the above conversion you have to convert all the integers during the runtime.
try this line
int Label11 =ToInt32.Parse(((Label)row.FindControl("Label11")).Text);

instead of this line
int Label11 =Convert.ToInt32(((Label)row.FindControl("Label11")).Text);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900