ddlProposalTypes' has a SelectedValue which is invalid because it does not exist in the list of items.

Last post 03-10-2008 6:08 AM by Hong-Gang Chen - MSFT. 1 replies.

Sort Posts:

  • ddlProposalTypes' has a SelectedValue which is invalid because it does not exist in the list of items.

    03-06-2008, 8:22 PM
    • Loading...
    • irbanjit
    • Joined on 09-11-2002, 11:38 AM
    • Posts 18

     

        PPDataContext db = new PPDataContext();

    db.ObjectTrackingEnabled = false;

    var eventTypes = from et in db.PP_Event_Types
    orderby et.SORT_ORDER, et.TITLE
    select new { eventType = et.EVENT_TYPE, title = et.TITLE };

    ddlProposalTypes.DataSource = eventTypes;
    ddlProposalTypes.DataValueField = "eventType";
    ddlProposalTypes.DataTextField = "title";
    ddlProposalTypes.DataBind();

    ListItem liBlank = new ListItem();
    liBlank.Value = "All";
    liBlank.Text = "All";

    ddlProposalTypes.Items.Add(liBlank);
    ddlProposalTypes.SelectedValue = "All";

    Error: ddlProposalTypes' has a SelectedValue which is invalid because it does not exist in the list of items. 

     I get this error at this line:  ddlProposalTypes.DataBind();

    I don't get any error the first time i go to this page, but after first time it's hit and miss.

     I am not even setting the selectedValues until later in the code.

    Any help is much appriciated!

  • Re: ddlProposalTypes' has a SelectedValue which is invalid because it does not exist in the list of items.

    03-10-2008, 6:08 AM
    Answer

    irbanjit:

     

        PPDataContext db = new PPDataContext();

    db.ObjectTrackingEnabled = false;

    var eventTypes = from et in db.PP_Event_Types
    orderby et.SORT_ORDER, et.TITLE
    select new { eventType = et.EVENT_TYPE, title = et.TITLE };

    ddlProposalTypes.DataSource = eventTypes;
    ddlProposalTypes.DataValueField = "eventType";
    ddlProposalTypes.DataTextField = "title";
    ddlProposalTypes.DataBind();

    ListItem liBlank = new ListItem();
    liBlank.Value = "All";
    liBlank.Text = "All";

    ddlProposalTypes.Items.Add(liBlank);
    ddlProposalTypes.SelectedValue = "All";

    Error: ddlProposalTypes' has a SelectedValue which is invalid because it does not exist in the list of items. 

     I get this error at this line:  ddlProposalTypes.DataBind();

    I don't get any error the first time i go to this page, but after first time it's hit and miss.

     I am not even setting the selectedValues until later in the code.

    Any help is much appriciated!

     

    Hi irbanjit,

    After test, your code works.

    So I think maybe your code likes following code, if so, please remove if(!IsPostBack), refer the following code:
     

        if (!IsPostBack)
            {

                DataClassesDataContext dcdc = new DataClassesDataContext();
                var eventTypes = from c in dcdc.Products
                                 select new { ID1 = c.CategoryID, ID2 = c.ProductID };
                this.DropDownList1.DataSource = eventTypes;
                this.DropDownList1.DataTextField = "ID1";
                this.DropDownList1.DataValueField = "ID2";
                this.DropDownList1.DataBind();
            }

    Hope it helps,

    Hong Gang
     

    Sincerely,
    Ocean
    Microsoft Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question.
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter