AKA Marketing.com Logo            VISIT THE BLOG            

Blogged thoughts, is our web blog. Expect views, opinion, rants and tirades about everything and anything 

« Home / Forums »        

Subscribe to our SEO / IT related blog by entering your email address below

Blogged thoughts

| by the www.akamarketing.com team



Cannot have multiple items selected in a DropDownList

I said I’d share this information about how to resolve this error since it took me almost a whole day to fix it. The ‘Cannot have multiple items selected in a DropDownList‘ exception will be thrown when the developer attempts to select a ListItem in a DropDownList when an item in that same DropDownList has already been selected earlier in previous code.

When searching on Google and the forums.asp.net site most of the suggested fixes to this problem suggested explicitly deselecting all selected items in a dropdown before selecting another. This can be done via something like ddl.clearselection() or ddl.SelectedIndex = -1. It seemed like a fairly logical approach, however it did not work for me, no matter what variation of deselection I tried.

On further investigation, it seemed the problem stemmed from the fact that I was adding the same ListItem to multiple dropdowns like so:

ListItem areaItem = new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString());
ddlArea.Items.Insert(i, areaItem);
ddlArea2.Items.Insert(i, areaItem);
ddlArea3.Items.Insert(i, areaItem);
ddlArea4.Items.Insert(i, areaItem);
ddlArea5.Items.Insert(i, areaItem);

but because a ListItem object is a reference object when I changed the selectedItem (and by doing so set a particular ListItems selected property to true) property of say ddlArea4 I also changed it in all the other dropdowns which contained the ListItem which was now selected. Remember reference objects do not keep a copy of an object they simply store a reference to the original object, thus a change in one dropdown list was leading to a change in all so code like the following which aims to explicitly deselect items before selecting another would not work:

ddlArea4.ClearSelection();
ddlArea4.SelectedIndex = -1;
ddlArea4.Items.FindByValue(itdp.Rows[2][1].ToString()).Selected = true; //the ListItem that this will select is present in multiple dropdowns, if any of them have anything selected an error will be thrown

The solution to the problem is to create new instances of ListItems for each option you need to add to dropdown lists, so instead of the first bit of code above, I tried something like:

ddlArea.Items.Add(new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString()));
ddlArea2.Items.Add(
new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString()));
ddlArea3.Items.Add(new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString()));
ddlArea4.Items.Add(
new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString()));
ddlArea5.Items.Add(new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString()));

It meant when I selected a particular ListItem in a particular dropdown only the one ListItem and dropdown was affected. It worked well and it even allowed me to remove the extra deselection logic which in this case it turned out I did not need.

This was quite an uncommon scenario that led to this problem and tracking down the solution was difficult. I think in over 90% of cases the problem with the “Cannot have multiple items selected in a DropDownList” error would come from the developer trying to select more than one item in a DropDownList in a more obvious way such as simply having two or more .Selected = true statements perhaps in different parts of his or her code without realising. Therefore always give something like ClearSelection() or SelectedIndex = -1 a go first before thinking too hard about this problem, it will most likely resolve it.

34 Comments on “Cannot have multiple items selected in a DropDownList”
1| steffen said,

Thanks a lot. This is great …
I was trying to fix this prob the hole day. Creating one instance of the ListItem for each dropdownlist works for me.

Thanks :)

2| Yogesh said,

Thanks a lot,

I was trying to get rid of this problem whole day.. Really great explaination about the fact that anyone rarely know….

Thanks again..

3| Richard said,

This saved me HOURS!!! Thanks for posting.

4| Jean Wright said,

I only wish I had found this this morning. Thank you!

5| Gary said,

thanks, this worked for me and the explanation clarified the nature of the problem

6| raju said,

thankx for ur help
just add ddlArea4.ClearSelection(); as u said.

7| Lacho said,

Exactly my problem! Thanks a lot!

8| Leonardo santos-Macias said,

My 5 cents, I started to get the same error when I migrated to .net 2.0 and 3.5, my code is quite complicated but works for sure with .net 1.1. I tried this solution and still get the same error on .net 2.0 and VS2008, downgraded back to 1.1, compiled with VS2003 and works fine without code modifications

9| Gonzo said,

Actually, this issue applies to any control that uses ListItem, not just dropdowns. And it’s not that uncommon for me since my client requires us to use “Yes” and “No” dropdowns instead of checkboxes so I might have 3 or more dropdowns with the same items in the same page. Another workaround is to populate your dropdowns like this (sorry, it’s VB):

Public Sub PopulateDropDowns2(ByVal sender As DropDownList)
Dim lo As ListItem
lo = New ListItem(”Select”, -1)
sender.Items.Add(lo)
lo = New ListItem(”No”, 0)
sender.Items.Add(lo)
lo = New ListItem(”Yes”, 1)
sender.Items.Add(lo)
End Sub

10| Patrick said,

I did not took me a whole day because of your article. Thanks a lot.

11| Colin said,

Excellent! You hit the nail on the head.

12| Tomas said,

Million thanks! After 2 days I was able to fix it instantly after reading your post. Thanks again.

13| Felipe said,

Man, THANKS!!! I was starting to pull my hair on this one! I created a blank ListItem to add at the beginning of each DataBound ddl. The funny thing is that this error came at first when a specific data came blank, which confused me even more. Any ways, this solved the problem and made my day. Thanks again!

14| D James said,

Let me add my thanks. I also was banging my head against a wall on this one so your comment was brilliant and solved my problem. Thanks.

15| Faiyaz said,

Excellent answer, was helfpul

16| Vic said,

Thanks a lot! You saved me hours!

17| Stig said,

Absolutely brilliant. Too me 4 hours to find what to look for, but this resolved my issue right away. Thx.

18| Dav said,

Thanks, Saved me a couple of hours!!

19| Franck said,

Thank you very much indeed!
Very helpful it save me a couple of hours too!

20| Jasdeep said,

Thank you a Lot!!!
I had tried all other options, but this worked for me!!!

21| WIL FROM COLOMBIA said,

Thanks a lot. I had hours trying to find a solution. You solved it…… Again Thanks….

22| J said,

THANK YOU!

23| IF said,

thanks a lot, i had all day try to solve it..

24| Sam said,

THANK YOU!!!
I could have spent another day on this !*@&#!! problem if I hadn’t read through this. You are absolutely right about the different new instances necessary. I am adding all my items dynamically, by necessity without using direct databinding. Once I created new instances of my generic top –Select– item, it fixed this MOST aggravating issue that made no sense.

25| Arun said,

Thanks a ton, was breaking my head for 2 days.

26| rho said,

Whoa, thanks! It has been bugging me a long time already. Also many thanks for the explenation.

27| Sam said,

Genius! Awesome! Brilliant!
I was wondering how selecting an item in one dropdownlist could modify the selected items of another dropdownlist. Well, you explained how.
It turns out I had a “dummy ListItem”, which I configured in a static readonly field to avoid duplicate code.
It all makes sense now.

28| Tyler said,

Thank you very much! I was adding the same ListItem to two different DropDownLists and could not figure out why I was getting errors until I found your post!

29| Vidya said,

Thanks a lot. Excellent.

30| Andy said,

Thanks a lot!
I really Appreciated, great listing saved me lots of time.

31| Pravin said,

i want add only unique records from my table to drop down list in asp.net 2.0 problem is that table contain same column value for multiple rows .
how is it do ?
Waiting for ur reply.

32| M said,

Excellent - saved me time

33| Alex said,

This was incredibly helpful and saved me a lot of time. Thank you.

34| Ginger said,

Oh my , thank you SO much, I have worked on this issue for more than 2 days…..none of the attempts to clear the selected value worked. And also it didn’t happen every time, now I see why. I thought I was being “efficient” by creating a blank ListItem called myBlankItem and using that for DDLB’s that were optional. I did not think about the object reference aspect and it explains why I could not reproduce it 100% of the time, since the other DDLB’s were set differently.

Leave a Comment
Name:
Email:
Website:
 
HOME | ABOUT US | CONTACT US | SITEMAP | GOOGLE SITE SEARCH | TOP
12 Lorcan Crescent, Santry, Dublin 9, Ireland +353 87 9807629