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();
    //}
1

1 Answer 1

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();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.