Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:17:36+00:00 2026-06-04T06:17:36+00:00

I am generating 10 labels on fly from code behind and I can see

  • 0

I am generating 10 labels on fly from code behind and I can see them when i run the page. But its giving an error when i try to access the label.text property. Its giving this error System.NullReferenceException: Object reference not set to an instance of an object.

for (int hf = 1; hf <= dfta2.Rows.Count; hf++)
                    {
                        Label lbl = new Label();
                        lbl.ID = "labeltest" + hf;
                        lbl.Text = "1";
                        lbl.ClientIDMode = System.Web.UI.ClientIDMode.Static;                                        
                        form1.Controls.Add(lbl);
                    }
     string variable3 = "labeltest" + i;

     Label lbl2 = form1.FindControl(variable3) as Label;

     lbl2.Text = "2";

i is just incremented by 1 every time I click the button. Any idea why its null ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:17:38+00:00Added an answer on June 4, 2026 at 6:17 am

    The code you’ve posted must be placed inside the Page_Load or Page_Init event and verify the value of variable i.

    just try,

    i=2;
    string variable3 = "labeltest" + i;
    Label lbl2 = form1.FindControl(variable3) as Label;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am generating some text in the code-behind of web page on button click
When generating models from postgresql with ColdFusion Illudium Code generator the boolean values get
The following code is generating an error on the variable con2 saying non-static variable
I am generating a page which renders a list of linkbuttons and labels. Now
I'm generating the user interface labels from database columns in one of my applications.
Generating normal columnar data in excel file is quite easy but does any one
Im generating a file to present to the user for download, but the server
I'm generating ics files ( iCalendar or RFC 2445 or however you call them)
I am generating a simple form with php. The following code has been reduced
Is there any existing software for generating a UserControl from a table of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.