0
votes

I have tried using FindControl which is what every single StackOverflow topic suggests, and I get an object reference error. Is there no other way? I found one topic where the guy seemed to be having the same problem as me but nobody answered his either.

I am trying to loop through my GridView and do a comparison, but row.Cells[3].Text returns a blank string. I assume this is because I have TextBox controls in my GridView, but when I try to access the TextBox controls OUTSIDE OF AN EDIT/UPDATE event I get an "Object reference not set to instance of object" error. It makes sense to me that I get this error because the TextBox is not "open" at the time I'm looping through the GridView.

So this is my dilemma. I can't get the value the ordinary way (row.Cells[3].Text) and I can't seem to get it by using FindControl either.

Is there some other way I'm supposed to retrieve the value in this case?

foreach (GridViewRow row in GridView1.Rows)
{
    if (row.Cells[3].Text == storeNumber)
    {
        // blah blah
    }
}

Alternatively,

foreach (GridViewRow row in GridView1.Rows)
{
    if (row.FindControl("StoreNumberLabel").ToString() == storeNumber)
    {
        // blah blah
    }
}

I have also tried casting the row.FindControl as a TextBox and using .Text instead of .ToString(), but same result.

I have read countless StackOverflow questions about related situations and I cannot find anything that relates to my specific question. If there is a topic that already addresses this, PLEASE link me. I have looked all over the place for a solution to this and cannot find anything.

Update: This is my aspx code

<asp:TemplateField HeaderText="Store">
    <ItemTemplate>
        <asp:Label ID="StoreNumberLabel" runat="server" Text='<%# Eval("Store") %>'>
        </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="storeNumberTB" runat="server" Text=' <%# Eval("Store")%>'></asp:TextBox>
    </EditItemTemplate>
</asp:TemplateField>

UPDATE2

I have sort of got it working. The trick is to check if row.FindControl("yourControl") is null. NOT the .Text of the TextBox, but just the row.FindControl().

Problem now is that it doesn't seem to be looking at every row. Only certain ones (which I'm guessing are for some reason the ones that aren't null?).

2
Can you share the html code of gridview?Chetan
Yes, please see update.Sherlock

2 Answers

1
votes

Your code has some issues...

First, row.Cells[3].Text will always be empty since it contains a Label, not text.

Second, you use ToString() on FindControl and not even casting it back to it's original type. So the value you compare storeNumber to will be Label.

Third, you are not checking if StoreNumberLabel exists before using ToString(), so that will trigger a referenece error if it is not found.

Four, you are probably expecting for StoreNumberLabel to exist in every row, but if the EditIndex is set it does not exist hence the exception. This is the same for storeNumberTB, it only exists when the EditIndex is set and then only in that row.

So you need to do this. Check for null and cast the control back to a Label.

if (row.FindControl("StoreNumberLabel") != null)
{
    Label lbl = row.FindControl("StoreNumberLabel") as Label;

    if (lbl.Text = storeNumber)
    {
        // blah blah
    }
}
0
votes

Did you try this??

GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
TextBox TextBox1 = row.FindControl("storeNumberTB") as TextBox; 
string storeNumberTBtext= TextBox1.Text;