0

In my code there is one gridview1 . While clicking on edit button getting error Object reference not set to an instance of an object. all my columns are template fields.id is the datakey. and I am able to fetch the datas from database too. What is wrong here. ?

  protected void Edit_Button_Click(object sender, EventArgs e)
    {
        GridViewRow gr = (GridViewRow)((Button)sender).NamingContainer;
        int id = Convert.ToInt32(GridView1.DataKeys[gr.RowIndex].Value);

        cmd = new SqlCommand("select * from students where id = '"+id+"'",con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        TextBox name = (TextBox)GridView1.Rows[gr.RowIndex].FindControl("name_TextBox");



        name.Text = "bhavin";
}

and this is my aspx page.

<asp:TemplateField HeaderText="Name">
                <EditItemTemplate>
                    <asp:TextBox ID="name_TextBox" runat="server" Text='<%# Eval("st_name") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("st_name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

.
.
.
.
<asp:TemplateField HeaderText="Action">
                <EditItemTemplate>
                    <asp:Button ID="Button3" runat="server" Text="Update" />
                    <asp:Button ID="Button4" runat="server" Text="Cancel" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Button ID="Edit_Button" runat="server" Text="Edit" OnClick="Edit_Button_Click" />
                    <asp:Button ID="Button2" runat="server" Text="Delete" />
                </ItemTemplate>
            </asp:TemplateField>
1
  • 2
    Well, did you step through the code in the debugger? It will tell you exactly what line is throwing the error. My guess is that FindControl is returning null. Commented Apr 16, 2015 at 11:30

1 Answer 1

1

Without your client side code (html) it is a bit hard.

But as you said you use templated columns.

Didn't you forget to place your textbox with ID="name_TextBox"in an EditItemTemplate?

like that :

   <EditItemTemplate> 
    <asp:TextBox id="name_TextBox"><asp:TextBox>
   </EditItemTemplate>

UPDATED ANSWER

The problem is you can't access control in Gridview_RowEditing. Controls will be rendered after this event (if my memory is still reliable : such a time I moved to MVC). If you manage this event, you have to bind gridview, ex :

 protected void YourGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
 {
   //you tell Gridview which row gonna be editing : 
   YourGridView.PageIndex = e.NewPageIndex;
   //then you Bind data to the GridView (as you bind data as you did on load for instance)
   YourGridView.DataSource = YourDataSetOrDataTable
   YourGridView.DataBind();
 }

If you want to access a control, you can use gridView_rowDataBound() event instead.

Sign up to request clarification or add additional context in comments.

1 Comment

I have added my aspx page code too.thanks for reply

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.