0

I am trying to bind data to a DropDownList that I created using the <asp:DropDownList tag. It successfully works for the first DropDownList, but an exception is thrown for the remaining two. I know that the data source is not null and does indeed have values, so the only thing I can think of is that the other two list boxes have not yet been loaded or bound.

I attempted to use the Page_LoadComplete method to add the content, but it doesn't seem to be firing. I'm used to MVC and I'm very new to Web Forms, so I'm not sure what I am doing wrong. Am I correct by stating that the elements have not yet been loaded, and if so how do I bind them once they are loaded?

protected void Page_Load(object sender, EventArgs e)
    {
        //Page.LoadComplete += new EventHandler(Page_LoadComplete);
        if (DataContext == null)
        {
            DataContext = new ReuseLibraryDataContext(SPContext.Current.Web.Url);
        }

        // top level category for all documents
        _functions = DataContext.ReuseLibrary.Select(p => p.FunctionalCategories).Distinct().ToList();

        // the first sub category
        _sc1 = DataContext.ReuseLibrary.Select(p => p.SubCategoriesLevel1).Distinct().ToList();

        // the second sub category
        _sc2 = DataContext.ReuseLibrary.Select(p => p.SubCategoriesLevel2).Distinct().ToList();


        // add the functions to the dropdown
        listFunctions.DataSource = _functions;
        listFunctions.DataBind();

        // add the sub cat 1 to the dropdown
        listSC1.DataSource = _sc1;
        listSC1.DataBind();

        // add the sub cat 2 to the dropdown
        listSC2.DataSource = _sc2;
        listSC2.DataBind();
    }

    //protected void Page_LoadComplete(object sender, EventArgs e)
    //{
    //    // add the functions to the dropdown
    //    listFunctions.DataSource = _functions;
    //    listFunctions.DataBind();

    //    // add the sub cat 1 to the dropdown
    //    listSC1.DataSource = _sc1;
    //    listSC1.DataBind();

    //    // add the sub cat 2 to the dropdown
    //    listSC2.DataSource = _sc2;
    //    listSC2.DataBind();
    //}
Cody
  • 8,061
  • 17
  • 65
  • 120
  • 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 Sep 20 '13 at 19:04

1 Answers1

3

I feel dumb - but I'll post what the issue was in case anyone else gets caught by something like this.

Even though I was supplying a list of data, any null objects in that list will also throw a NullReferenceException. Quite understandable.

To solve I removed the null values, and viola. Works as expected.

_sc1.RemoveAll(item => item == null);
_sc2.RemoveAll(item => item == null);

I ended up updating the query to exclude null values:

_sc1 = DataContext.ReuseLibrary.Select(p => p.SubCategoriesLevel1).Where(p => p != null).Distinct().ToList();

Cody
  • 8,061
  • 17
  • 65
  • 120