Friday, June 22, 2012

ASP.NET: DropDownList throws an exception “[ctlName] has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value”

 

Yes, you may monitor this exception if your DropDownList WebControl is initialized programmatically, like

ddlDocumentState.DataValueField = "StateId";
ddlDocumentState.DataTextField = “StateName";
ddlDocumentState.DataSource = stateList;
ddlDocumentState.DataBind(); // <— here is exception thrown

If the data source doesn’t include an element with empty string as value, the above mentioned exception is thrown.

The problem is solved with setting SelectedValue of the DropDownList to null:

ddlDocumentState.DataValueField = "StateId";
ddlDocumentState.DataTextField = “StateName";
ddlDocumentState.SelectedValue = null; // <- added to solve
 

                                       // the problem
ddlDocumentState.DataSource = stateList;
ddlDocumentState.DataBind(); // <— here is no more exception

Enjoy!

18 comments:

  1. Anonymous3:35 AM

    Excellent post. I learned a lot reading it. Thanks.

    ReplyDelete
  2. Anonymous9:36 PM

    This will probably be the proper weblog in case you truly wants to look at this topic.

    ReplyDelete
  3. Anonymous1:07 PM

    Say, you got a good article. Much thanks again. Wonderful.

    ReplyDelete
  4. Anonymous3:28 PM

    Hi there. I discovered your website via Google even as looking for a related matter, your web site got here up. It appears great.

    ReplyDelete
  5. Anonymous6:30 PM

    I want reading and I think this website got some really utilitarian stuff on it!

    ReplyDelete
  6. Anonymous11:11 AM

    Quick question that's totally off topic. Do you know how to make your site mobile friendly?

    ReplyDelete
  7. @anonymous: check out microsoft guidlines for mobile web app using asp.net. keywords: skins, styles, browser dependend content, adapters.
    mail me for more info

    ReplyDelete
  8. Anonymous1:15 PM

    Appreciate your sharing this greatest doc.

    ReplyDelete
  9. Anonymous1:20 AM

    Hey There. I found your weblog the usage of msn. This is a very well written article.

    ReplyDelete
  10. Anonymous6:00 PM

    You have many managed to impress me by writing this thought-provoking informational article.

    ReplyDelete
  11. Anonymous9:36 AM


    I am not sure where you are getting your information, but great topic.

    ReplyDelete
  12. Anonymous6:50 AM


    Thanks for such a great post and the review, I am totally impressed!

    ReplyDelete
  13. Anonymous3:00 AM


    I enjoyed this article. It was inspiring. Keep on posting!

    ReplyDelete
  14. Anonymous10:52 PM


    Outstanding post, I conceive website owners should acquire a lot from this web site its really user pleasant. So much great information on here

    ReplyDelete
  15. Anonymous6:01 PM


    Hiya. Very nice web site!!

    ReplyDelete
  16. Anonymous5:00 AM


    I was just seeking for this information for a while.

    ReplyDelete
  17. Thanks For Your Information... Keep Posting

    ReplyDelete