---
title: "FindControl in gridview returns null"  
description: "FindControl in gridview returns null"  
author: "Anonymous User"  
published: 2014-08-18  
updated: 2014-08-18  
canonical: https://www.mindstick.com/forum/2051/findcontrol-in-gridview-returns-null  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# FindControl in gridview returns null

I am currently in a dilemma with my [gridview](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) not [returning](https://www.mindstick.com/news/2477/returning-ceo-bob-iger-dismisses-the-disney-apple-sale-as-pure-speculation) a [label](https://www.mindstick.com/articles/12835/print-label-tracking-how-do-these-features-make-auspost-woocommerce-plugin-better), which is within a [detailsview](https://www.mindstick.com/forum/12737/get-the-value-of-a-boundfield-from-a-detailsview)...

**My [C# code](https://www.mindstick.com/forum/403/how-to-export-data-in-excel-file-from-sql-server-using-c-sharp-code) is:**

```
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){    // get pet number for when removing a pet from reservation    int numberSelected = -1;    String numbertxt = "-1";    GridView gv1 = (GridView)sender;    GridViewRow rvRow = gv1.Rows[gv1.SelectedRow.RowIndex];    Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");    // find selected index, and get number in column 0    // label within GridView1 within dvReservation DetailsView    numbertxt = numberLbl.Text;
```

...

## Gridview:

```
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"    DataSourceID="dsObjGet"    OnSelectedIndexChanged="GridView1_SelectedIndexChanged">    <Columns>        <asp:TemplateField InsertVisible="False" ShowHeader="False">            <AlternatingItemTemplate>                <asp:Label ID="lblNumber" runat="server"                    Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>            </AlternatingItemTemplate>            <ItemTemplate>                <asp:Label ID="lblNumber" runat="server"                    Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>            </ItemTemplate>        </asp:TemplateField>        <asp:TemplateField ShowHeader="False">            <AlternatingItemTemplate>                <asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>                </asp:Label>            </AlternatingItemTemplate>            <ItemTemplate>                <asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>                </asp:Label>            </ItemTemplate>        </asp:TemplateField>        <asp:CommandField SelectText="Remove" ShowSelectButton="True"            CausesValidation="False">            <ControlStyle CssClass="link" />        </asp:CommandField>    </Columns></asp:GridView>
```

When I breakpoint

Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");

the label comes out as null (numberLbl)...

The [message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) returned from the [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) is: "Object [reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) not set to an [instance of an object](https://www.mindstick.com/forum/376/system-nullreferenceexception-object-reference-not-set-to-an-instance-of-an-object)"

## Replies

### Reply by Pravesh Singh

Hi Jeet, \

```
GridView gv1 = (GridView)sender;GridViewRow rvRow = gv1.SelectedRow;Label numberLbl =(Label)rvRow.FindControl("lblNumber");
```


---

Original Source: https://www.mindstick.com/forum/2051/findcontrol-in-gridview-returns-null

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
