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:

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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

winmike said...

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

Anonymous said...

Appreciate your sharing this greatest doc.

Anonymous said...

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

Anonymous said...

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

Anonymous said...


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

Anonymous said...


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

Anonymous said...


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

Anonymous said...


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

Anonymous said...


Hiya. Very nice web site!!

Anonymous said...


I was just seeking for this information for a while.

bigbang said...

Thanks For Your Information... Keep Posting

Inger Hockaday said...

Cool!! Keep Posting