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 8167011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:04:19+00:00 2026-06-06T20:04:19+00:00

I have a table that includes 27 DropDownLists for user input. My table has

  • 0

I have a table that includes 27 DropDownLists for user input. My table has 27 occurrences of this HTML:

<span id="s1" runat="server"><asp:PlaceHolder ID="p1" runat="server"></asp:PlaceHolder></span>

where the spans are indexed s1, s2, …, s27 and the PlaceHolders are indexed p1, p2, …, p27. The reason that the spans are indexed is so that I can replace the DropDownList with whatever selection was made — ie, the DropDownList will disappear.

Here is how I am generating the DropDownLists:

protected void Page_Load(object sender, EventArgs e)
{
    var data = CreateDataSource();
    int x;
    for (x = 1; x <= 27; x++)
    {
        DropDownList dl = new DropDownList();
        string index = x.ToString();
        dl.ID = "TrendList" + index;
        dl.AutoPostBack = true;
        dl.SelectedIndexChanged += new EventHandler(this.Selection_Change);
        dl.DataSource = data;
        dl.DataTextField = "TrendTextField";
        dl.DataValueField = "TrendValueField";
        dl.DataBind();
        if (!IsPostBack)
        {
            dl.SelectedIndex = 0;
        }
        PlaceHolder ph = (PlaceHolder)form1.FindControl("p" + index);
        ph.Controls.Add(dl);
    }
}

A runtime error occurs at the last line. I can select any DropDownList I want and make a selection, but when I select a second DropDownList and make a selection I get this error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Line 46:             }
Line 47:             PlaceHolder ph = (PlaceHolder)form1.FindControl("p" + index);
Line 48:             ph.Controls.Add(dl);
Line 49:         }

This seemed to be working when I was doing it by brute force:

p1.Controls.Add(DropList1);
p2.Controls.Add(DropList2);
etc....

but now I am getting an error. I have run this in the debugger but I can’t find a null reference.

Any advice is appreciated.

Regards.

  • 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-06T20:04:20+00:00Added an answer on June 6, 2026 at 8:04 pm

    The problem turned out to be that this function was called on every new page. Meaning that after the first run the first placeholder no longer existed and threw the null reference error. The problem was solved with this code:

    if (placeHolder != null)
    {
        placeHolder.Controls.Add(ddl);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of userdata that includes user ID and category IDs. I
SQL Server 2000. Single table has a list of users that includes a unique
I have a table that includes a column for the user to select from
I have a table of user-generated content that includes an ENUM format column for
I have a table in which one column (type ntext) has data that includes
I have a table that includes some student group name, lesson time, day names
I have a table it includes 3 foreign key field like that: My Table:
I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a mysql table that includes the fields: Name - Year - Description

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.