Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is the ccode i m using under gridview rowupdating event:


C#
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?
Posted
Comments
a2ulthakur 7-Feb-13 2:24am    
help !!!!

1 solution

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!
 
Share this answer
 
Comments
a2ulthakur 7-Feb-13 2:31am    
ankur my autoid field is an int field an autoincrement field
Ankur\m/ 7-Feb-13 3:02am    
And you are assigning value to that field from a Label in your HTML page. The text that Label contains is not a valid int. IT could be blank or a number with space. Add a watch for ((Label)GridView1.Rows[e.RowIndex].FindControl("Label12")).Text
and see what it returns.
a2ulthakur 7-Feb-13 2:34am    
and how to use Int32.TryParse method in my above mentioned line??
Ankur\m/ 7-Feb-13 3:03am    
Click on the first overload link which is http://msdn.microsoft.com/en-us/library/f02979c7.aspx
You will find a usage example there.
a2ulthakur 7-Feb-13 3:29am    
when i am doing [ ((Label)GridView1.Rows[e.RowIndex].FindControl("Label12")).Text] its saying cannot implicitly convert type 'string'to 'int'

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