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

  • Question

  • User-31108246 posted

     

        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!

    Thursday, March 6, 2008 8:22 PM

Answers

  • User-2115483147 posted

     

        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:
     

    <strike>    if (!IsPostBack)
            {</strike>
                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();
    <strike>        }</strike>

    Hope it helps,

    Hong Gang
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 10, 2008 6:08 AM