1

I want to add a row to my gridview. i succeeded in adding text boxes, but i cannot extract the value It keeps telling me that object reference not set to an instance of an object. At this line it halts

string acc = Convert.ToString(((TextBox)GridView1.FooterRow.FindControl("accountID")).Text);

Please what am i doing wrong

2
  • 1
    You don't need to convert a string to a string(TextBox.Text returns already a string). How and where have you added the TextBox to the footer-row? Commented Nov 26, 2012 at 15:41
  • i added the textBox to the footer row in the method below protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) Commented Nov 26, 2012 at 15:46

2 Answers 2

1
  1. You don't need to convert a string to a string(TextBox.Text returns already a string).

How and where have you added the TextBox to the footer-row?

i added the TextBox to the footer row in GridView1_RowDataBound

RowDataBound isn't the right method for dynamic controls since it is called only on databinding and not on every postback. But dynamical controls need to be recreated on every postback.

So use RowCreated instead to create controls dynamically and use RowDataBound to databind them.

protected void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        TextBox tb = new TextBox();
        tb.ID = "accountID";
        e.Row.Cells[indexOfColumn].Controls.Add(tb);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

so how do i use rowcreated. First to bind the textboxes that allows the user to enter the new data, secondly to capture data entered by the user into a database
@mayowaogundele: Edited my answer to privide an example. Note that indexOfColumn is the index of the column where you want to add the TextBox.
0

Ok first check wether its a footer row ,then find the textBox in it

protected void grdAccounts_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.Footer)
  {
   //get text box value here
  }
}

In Button Click

try this

GridViewRow row = GridView1.FooterRow; 
firstName = ((TextBox)row.FindControl("TextBox1")).Text;

2 Comments

but i think i am supposed to retrieve the content of the text on button click. So put the above code in _RowCommand and i was having errors
it still gives NullReferenceException after the textbox has been filled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.