Hitesh Gawhade
Hitesh Gawhade

Reputation: 181

Cannot update Value of textbox in Gridview : Object reference not set to instance of an object

I am using edit template in my gridview to update some control values like dropdowns, textboxes etc.I am finding these control on update function as follows :

string dd1 = ((DropDownList)OwnerGrid.Rows[e.RowIndex].FindControl("ddl1")).SelectedItem.Text.Trim();

string actual = ((TextBox)OwnerGrid.Rows[e.RowIndex].FindControl("txtowneractual")).Text.Trim();

I have bound dropdowns from which i am selecting items before updating. I am also filling textboxes before updating. The textboxes are not binded.

When I am clicking on update, it is throwing "Object reference not set to instance of an object" error. I have debugged the code, textbox value is null while I am getting dropdown value.

What is the issue ?

Designer for textbox :

<asp:TemplateField HeaderText = "Actual" >

<EditItemTemplate>
                <asp:TextBox ID="txtowneractual" Width="80px" runat="server" ></asp:TextBox>                  

</EditItemTemplate>
<ItemTemplate>               

</ItemTemplate>
</asp:TemplateField>

Upvotes: 2

Views: 2784

Answers (2)

Waqar Janjua
Waqar Janjua

Reputation: 6123

I think your textbox is in Edit Item template due to which you are getting a null reference exception. try some thing like this

if (e.Row.RowState == DataControlRowState.Edit )
 {
string actual = ((TextBox)OwnerGrid.Rows[e.RowIndex].FindControl("txtowneractual")).Text.Trim();
 }

Upvotes: 2

Narendra
Narendra

Reputation: 3117

Check if id of your textbox is "txtowneractual".

And use the following code instead.

TextBox txtOwnerActual = (TextBox)OwnerGrid.Rows[e.RowIndex].FindControl("txtowneractual");

if(txtOwnerActual  != null)
{
       string actual = txtOwnerActual.Text.Trim();
}

Upvotes: 1

Related Questions