c# - Dropdownlist has a SelectedValue which is invalid because it does not exist in the list of items. And Dropdownlist databind not working -


what doing clearing items of dropdownlist , adding 3 items it. @ first encountered (refer title) error. resolved issue these links: "asp.net web forms dropdownlist has selectedvalue invalid because not exist in list of items" "dropdownlist "has selectedvalue invalid because not exist in list of items"" i.e made dropdown.selectedvalue=null; , added databind() before adding items. not getting error items not getting bound. . here code:

ddl_org_type.items.clear(); ddl_org_type.selectedvalue = null; ddl_org_type.databind(); ddl_org_type.items.insert(0, new listitem("select","0")); ddl_org_type.items.insert(1, new listitem("bangalore one", "b1")); ddl_org_type.items.insert(2, new listitem("s1 service centre", "sc")); ddl_org_type.selectedindex = 0; ddl_org_type.databind(); 

but if try :
ddl_org_type.selectedvalue = "0"; after binding items, throws (title) error. not know going wrong. want bind items dropdownlist. please. thank you.

first add items dictionary , bind drop down list

example:

dictionary <string, string> dic = new dictionary<string, string>(); dic.add("select","0"); dic.add("bangalore one", "b1"); dic.add("s1 service centre", "sc");  ddl_org_type.datasource = new bindingsource(dic, null);   ddl_org_type.displaymember = "value"; ddl_org_type.valuemember = "key"; 

Comments