posts - 236, comments - 436, trackbacks - 56

My Links

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

Archives

Free Programming Language Training

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

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.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Saturday, August 23, 2008 4:50 PM |

Feedback

Gravatar

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

Thank you very much!
8/28/2008 3:31 AM | mp
Gravatar

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

Thnx Shahed,
this tip solvedmy propblem!!

Thnx
prasad punneri
3/17/2009 4:55 PM | prasad punneri
Gravatar

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

Thank you very much! Saved me much frustration.
4/7/2009 6:14 AM | hmkjr
Gravatar

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

Good Tip!

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

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

Thanks that works for me
7/15/2009 3:08 AM | SudershanB
Gravatar

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

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 -:)






8/19/2009 3:39 AM | Ramakrishna
Gravatar

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

hi i find sollution from this page and very thanx for this ideas....
9/7/2009 10:12 PM | harry
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: