0

I wanted to update my table through gridview. [Visual Studio 2010]

For that i have done following:

Gridview>> EditColumns >> CommandField >> Edit,update,cancel Added to grid.

Gridview>> EditTemplates >> Added textbox names "TextBox1">>End Editing Template.

Row Editing Event:

public int i;
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
   i=gv.EditIndex = e.NewEditIndex;
}

Row Updating Event:

protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                TextBox txtSymbol;

                txtSymbol = ((TextBox)(gv.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")));

                con.Open();
                cmd = new SqlCommand("update temp set Symbol=@Symbol", con);
                cmd.Parameters.AddwithValue("@Symbol",txtSymbol.Text);
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
            }
        }

I refered this code from here.

Here its mentioned that th line should be like this:

txtSymbol = ((TextBox)(gv.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")));

All this coded thing is not working.

`((TextBox)(gv.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")))` 

is having null value.

Gives exception in Row Updating event as "Null Referance Exception."

What is wrong in my code?

Where i am making mistake.

Please guid me.

4

1 回答 1

1
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
   gv.EditIndex = e.NewEditIndex;
   BindGridView();//You have  to  Bind GridView Again Here...
}

If you dont bind the Gridview Again After setiing it Edit Index your control is not bounded With gridview.Thats why it giving you null refrence exception.

于 2013-05-15T12:30:23.260 回答