2

Possible Duplicate:
What is a NullReferenceException in .NET?

I have a bunch of Labels on a single page that need to be updated when the page Loads. I know the labels' Ids are "Label1" - "Label8", however when i run the following snippet, I get "Object reference not set to an instance of an object." on the Label.Text line, so I'm assuming that the Label cannot be found.

int i = 1;
foreach (string sel in selArr)
{
    string labelId = "Label" + i.ToString();
    MySqlCommand cmd = new MySqlCommand(sel, conn);
    MySqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    { 
       Label label = (Label)FindControl(labelId);
       label.Text = "( " + reader["c"] + " )";
    }
    reader.Close();
    i++;
}

I haven't really tried much else because I'm still pretty new to C#.

Community
  • 1
  • 1
smarble
  • 335
  • 4
  • 16
  • Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 21 '12 at 18:21

4 Answers4

3

The FindControl method only searches through the top-layer of the page. It does not do a recursively search through the whole page.

Maarten
  • 21,254
  • 3
  • 44
  • 62
  • Thanks, I guess i'll have to figure something out, maybe go the long route and do a switch or something. – smarble Dec 21 '12 at 17:54
1

if you know where the labels are, tell C#, jim has documented a good explanation I believe, it's not exactly your answer but may give you the idea.

Community
  • 1
  • 1
Mahdi Tahsildari
  • 11,900
  • 14
  • 50
  • 89
1

You can access your form Id's like what you did.May be b'cos of some kind of corruption that can be happened.Try below ones.

Solution 1 :

Check whether your designer.cs file having below kind of lines for your labels.If it's not there then add it manually.

protected global::System.Web.UI.WebControls.Label Label3; 

Solution 2

  1. Delete the designer.cs file
  2. right click the parent aspx file and choose "Convert to Web Application"
  3. This should recreate the designer.cs file with all the appropriate entries

I hope this will help to you.

Sampath
  • 50,641
  • 40
  • 250
  • 357
  • Except when "Convert to Web Application" is not an available option. – vapcguy Jun 02 '16 at 17:05
  • I found if you just rebuild your project, without deleting the designer.cs file, it will rebuild designer.cs, too. If you delete it, it doesn't. – vapcguy Jun 02 '16 at 21:42
0

If your label controls are within another control, then you can use the following method to recurse through all of the controls in a page to find your target:

protected Control RecursiveFindControl(Control targetControl, string findControlId)
    {
        if (targetControl.HasControls())
        {
            foreach(Control childControl in targetControl.Controls)
            {
                if (childControl.ID == findControlId)
                {
                    return childControl;
                }

                RecursiveFindControl(childControl, findControlId);
            }
        }

        return null;
    }

To use, it would be something like this:

Label label = (Label)RecursiveFindControl(Page, labelId);
Charles Wesley
  • 828
  • 12
  • 27