Geeks With Blogs

News Awarded Microsoft MVP C#.NET - 2007, 2008 and 2009


I am born in Bangladesh and currently live in Melbourne, Australia. I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net)and born in Bangladesh.
I am founder and Chief Executive Officer of
Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Co-founder and core developer of Pageflakes www.pageflakes.com.
Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta realestatebazaar.com.bd an ASP.NET MVC application written in C#.NET.


Some of My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Shahed Khan blog

Problem
I was facing the following Exception when I was programmatically assigning Selected Value of a DropDownList with the following piece of code.

ListItem item = DropDownListTest.Items.FindByValue("Test");
  if (item != null)
     item.Selected = true;

Exception

Cannot have multiple items selected in a DropDownList.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Cannot have multiple items selected in a DropDownList.
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): Cannot have multiple items selected in a DropDownList.]
   System.Web.UI.WebControls.DropDownList.VerifyMultiSelect() +107
   System.Web.UI.WebControls.ListControl.RenderContents(HtmlTextWriter writer) +1825978
   System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
   System.Web.UI.Control.Render(HtmlTextWriter writer) +7
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   CSSFriendly.WizardAdapter.RenderStep(HtmlTextWriter writer, Wizard wizard) +137
   CSSFriendly.WizardAdapter.RenderContents(HtmlTextWriter writer) +93
   System.Web.UI.WebControls.Adapters.WebControlAdapter.Render(HtmlTextWriter writer) +23
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +2116097
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
   System.Web.UI.Control.Render(HtmlTextWriter writer) +7
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
   System.Web.UI.UpdatePanel.RenderChildren(HtmlTextWriter writer) +236
 

Solution

The solution is to remember to call ClearSelection() Method before calling FindByValue("") or FindByText("") Method.

The following piece of code will work fine.

DropDownListTest.ClearSelection();

ListItem item = DropDownListTest.Items.FindByValue("Test");
  if (item != null)
     item.Selected = true;

I also noticed, I do not get the "Cannot have multiple items" exception when I use SelectedIndex instead. i.e.

DropDownListTest.SelectedIndex = 12;


Hope this saves some of your time. Happy Coding.

Posted on Saturday, August 23, 2008 4:50 PM | Back to top


Comments on this post: ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"

# re: ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"
Requesting Gravatar...
Thank you very much!
Left by mp on Aug 28, 2008 3:31 AM

# re: ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"
Requesting Gravatar...
Thnx Shahed,
this tip solvedmy propblem!!

Thnx
prasad punneri
Left by prasad punneri on Mar 17, 2009 4:55 PM

# re: ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"
Requesting Gravatar...
Thank you very much! Saved me much frustration.
Left by hmkjr on Apr 07, 2009 6:14 AM

# re: ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"
Requesting Gravatar...
Good Tip!

Alternatively, you could code it this way to avoid this problem:
if (DropDownListTest.Items.FindByValue("Test") != null)
DropDownListTest.SelectedValue=”Test”;
Left by Justin Saraceno on May 15, 2009 5:58 AM

# re: ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"
Requesting Gravatar...
Thanks that works for me
Left by SudershanB on Jul 15, 2009 3:08 AM

# re: ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"
Requesting Gravatar...
Here is the one more scenario and solution provided

If we add same list item to object to different dropdowns and trying to set selectedIndex for different dropdowns the same error it will throw

like
ListItem _listitemDefault=new ListItem(" - Select -","0");

//Wrong Code -throws Cannot have multiple items selected in DropDownList error
dropdown1.items.insert(0,_listitemDefault);
dropdown2.items.insert(0,_listitemDefault);

dropdown1.selectedIndex=1;
dropdown2.selectedIndex=2;

Solution

dropdown1.items.insert(0,new ListItem("-Select-","0"));
dropdown2.items.insert(0,new ListItem("-Select-","0");

dropdown1.selectedIndex=1;
dropdown2.selectedIndex=2;

Happy Coding -:)






Left by Ramakrishna on Aug 19, 2009 3:39 AM

# re: ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"
Requesting Gravatar...
hi i find sollution from this page and very thanx for this ideas....
Left by harry on Sep 07, 2009 10:12 PM

Your comment:
 (will show your gravatar)


Copyright © Shahed Khan | Powered by: GeeksWithBlogs.net