ASP.NET Dropdownlist - "Cannot have multiple items selected in a DropDownList."

Rate It (2)

Last post 10-15-2007 12:21 AM by sandeeppandana. 9 replies.

Sort Posts:

  • ASP.NET Dropdownlist - "Cannot have multiple items selected in a DropDownList."

    05-03-2006, 6:41 AM
    • Star
      12,063 point Star
    • ranganh
    • Member since 02-11-2004, 11:35 PM
    • India
    • Posts 2,428

    Hi,

    You may receive the error "Cannot have multiple items selected in a DropDownList." when trying to set a value selected in an ASP.NET Dropdownlist.

    This error specifically occurs if you are trying to execute a similar code as follows:-



    DropDownList1.Items.FindByValue("3").Selected = true;

    (or)

    DropDownList1.Items.FindByText("Sports").Selected = true;

    This error doesnt occur if you try to use

    DropDownList1.SelectedIndex = 3;
    or
    DropDownList1.SelectedItem.Value = "3"; (Wrong way of implementation. But people use this)


    The resolution is that, you just have to mention DropDownList1.ClearSelection(); before using D
    ropDownList1.Items.FindByValue("3").Selected = true;

    The correct step is as below:-

    DropDownList1.ClearSelection();
    DropDownList1.Items.FindByValue("3").Selected = true;

    or

    DropDownList1.ClearSelection();
    DropDownList1.Items.FindByText("Sports").Selected = true;

    Thanks.

    regards,
    Harish

    http://geekswithblogs.net/ranganh
  • Re: ASP.NET Dropdownlist - "Cannot have multiple items selected in a DropDownList."

    05-03-2006, 3:39 PM
    • All-Star
      18,370 point All-Star
    • PeterBrunone
    • Member since 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,683
    • TrustedFriends-MVPs
    With 1.1, you can just say

    DropDownList1.SelectedValue = "sports"

    instead.

    Of course you've always been able to do this with ELB, and the selection is cleared for you automatically :)

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • File changed in different computers

    05-04-2006, 11:21 AM
    • Member
      330 point Member
    • Jessy
    • Member since 12-30-2005, 7:31 PM
    • Posts 157
    Hi expert. I wrote a program using asp.net. I have run this program for serveral months and everything is fine. Later on  I changed a new computer, the program still ran but the printout has changed significantly no matter how I changed the setting and codes. What caused the problem? Also I copied this file to the other computer and it worked fine at that time, but one month later, I tried to run the program again, it doesn't work any more and it gives me a bunch of  web system errors. These two computers are new ones. As normal, it shouldn't happen, but it did. I really don't know what's wrong with them. Appreciate for your kindly help!
  • Re: ASP.NET Dropdownlist - "Cannot have multiple items selected in a DropDownList."

    04-16-2007, 9:26 AM
    • Member
      point Member
    • Nitin Pawar
    • Member since 02-20-2007, 10:03 AM
    • Mumbai
    • Posts 12

    Hi All,

    I am having same problem but with different code.

    I have 3 dropdown box, named ddDay, ddMonth, ddYear.
    In the page i am using javascript for date validation using Custom Validation Control

    Javascript
    =========================================================

    function findObj(n, d) { //v4.01
      var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
      if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
      if(!x && d.getElementById) x=d.getElementById(n); return x;
    }

    function getDDValue(idDD)
    {
        var objDD = findObj(idDD); // findObj returns object from id
        return objDD.options[objDD.selectedIndex].value;
    }

    function jsCheckDate(sender, args)
    {
        var ddDay = getDDValue("<%=ddDay.ClientID%>");
        var ddMonth = getDDValue("<%=ddMonth.ClientID%>");
        var ddYear = getDDValue("<%=ddYear.ClientID%>");
     
        var strDate = ddDay + "-" + ddMonth + "-" + ddYear;

     if (isDate(strDate, "d-MMM-y")) // isDate is function to check date
        {
            args.IsValid = true;
            return;
        }else {
            args.IsValid = false;
            return;
        }
    }

    Code Behind (On pageload)
    =========================================================

           If Not IsPostBack Then

                ddDay.Items.Add(New ListItem("-- Day --", ""))
                ddDay.SelectedIndex = 0
                For i As Integer = 1 To 31
                    ddDay.Items.Add(i)
                Next

                ddMonth.Items.Add(New ListItem("-- Month --", ""))
                ddMonth.SelectedIndex = 0
                For i As Integer = 1 To 12
                    ddMonth.Items.Add(New ListItem(MonthName(i), MonthName(i, True)))
                Next

                ddYear.Items.Add(New ListItem("-- Year --", ""))
                ddYear.SelectedIndex = 0
                For i As Integer = (Year(Now) - 50) To (Year(Now) - 15)
                    ddYear.Items.Add(i)
                Next

            End If

     

    Error Page
    =========================================================

    Server Error in '/Quincy' Application.

    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:

    Line 26:     var ddDay = getDDValue("<%=ddDay.ClientID%>");
    Line 27:     var ddMonth = getDDValue("<%=ddMonth.ClientID%>");
    Line 28:     var ddYear = getDDValue("<%=ddYear.ClientID%>");
    Line 29:   
    Line 30:     var strDate = ddDay + "-" + ddMonth + "-" + ddYear;

    Source File: C:\Inetpub\wwwroot\Quincy\Consultants\Profile_Main.aspx    Line: 28

    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) +1774854
       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
       ASP.consultants_profile_main_aspx.__RenderContent1(HtmlTextWriter __w, Control parameterContainer) in C:\Inetpub\wwwroot\Quincy\Consultants\Profile_Main.aspx:28
       System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +2065851
       System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
       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
       ASP.consultants_consultant_master.__RendertdContent(HtmlTextWriter __w, Control parameterContainer) in C:\Inetpub\wwwroot\Quincy\Consultants\Consultant.master:61
       System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +2065851
       System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
       System.Web.UI.HtmlControls.HtmlContainerControl.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) +130
       System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
       System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +59
       System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +68
       System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
       System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +37
       System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +130
       System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
       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) +130
       System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
       System.Web.UI.Page.Render(HtmlTextWriter writer) +26
       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.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1896
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

  • Re: ASP.NET Dropdownlist - "Cannot have multiple items selected in a DropDownList."

    04-17-2007, 11:38 AM
    • All-Star
      18,370 point All-Star
    • PeterBrunone
    • Member since 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,683
    • TrustedFriends-MVPs
    So what was your solution?
    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: ASP.NET Dropdownlist - "Cannot have multiple items selected in a DropDownList."

    04-24-2007, 9:49 AM
    • Participant
      823 point Participant
    • kennsterdude
    • Member since 01-27-2003, 3:17 PM
    • Tampa, FL
    • Posts 167

    Hello:

    I am having a similar issue whereby identical code & database running on two separate machines works on one and not the other. In my case, I upgrade a DNN 4.4.1 install to DNN 4.5.1 on two separate machines running an identical stack of software (I am working on a modification of the core store module of DotNetNuke for a client of mine). The module uses some MVP architecture principals and is very sophisticated, so sophisticated in fact that sometimes it is very hard to trace though to find the offending code.

     I also get a similar stack trace : thoroughly incomprehensible for me.

    I read somewhere this may have to do with required field validators: and there are RFV's in the module, which are selectively enabled based upon internationalization options.

    The main point I wanted to make is that of two identical stacks of software, only one exhibits this problem.

     One more note: Asp.net 2.0 with ajax extensions installed on the machine that doesn't work.

     

  • Re: ASP.NET Dropdownlist - "Cannot have multiple items selected in a DropDownList."

    09-25-2007, 6:57 AM
    • Member
      27 point Member
    • Subhadip
    • Member since 09-14-2006, 11:49 AM
    • Posts 14

    You cannot have more than 1 item selected in the dropdown at the same time, hence the current selected item should be made selected  = false and then make the item you want to be selected as true

    The solution to the above error is:

    First...

    /// Make the current item deselected

    dropdownInstance.SelectedItem.Selected = false;

    then the following piece:

    dropdownInstance.FindByValue("xyz").Selected = true;

    or

    dropdownInstance.FindByValue("xyz").Selected = false;

    Let me know if this solves the issue.

     

    Thanks,

    Subhadip

  • Re: ASP.NET Dropdownlist - "Cannot have multiple items selected in a DropDownList."

    09-25-2007, 9:52 AM
    • All-Star
      18,370 point All-Star
    • PeterBrunone
    • Member since 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,683
    • TrustedFriends-MVPs

    Welcome to the thread!

    If you don't appear to be setting multiple items to Selected = True, then you may have another problem.  Recently I came across people who would create new ListItem objects and try to add them to multiple list controls, or add them multiple times without instantiating a new ListItem each time.  This can lead to multiple selected items, and trigger the error mentioned here.
     

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: ASP.NET Dropdownlist - "Cannot have multiple items selected in a DropDownList."

    10-10-2007, 5:49 AM
    • Member
      68 point Member
    • ashwani2kumar
    • Member since 08-10-2007, 9:04 AM
    • Delhi, India
    • Posts 83

    Hello

     So i simple words you want to pass the value from ddl to SQL query hmm ok...

    here :

    Your code...

    Dim cmd12 As New SqlCommand("SELECT Cgrade from access_control.dbo.table_cadre" & _
    " WHERE cadre = '" & ddl_cadre.SelectedItem.Text & "' ", consql)

    cmd12.Connection.Open()
    reader = cmd12.ExecuteReader()
    ddl_cgrade.DataSource = reader
    ddl_cgrade.DataTextField = "Cgrade"
    ddl_cgrade.DataBind()
    reader.Close()
    cmd12.Connection.Close()

    follow my code :
    Dim CMD as SQLCommand

    Cmd.CommandText = "Select Cgrade from access_control.dbo.table_cadre" & _
    "Where cadre='" & ddl_cadre.SelectedValue.ToString & "'", consql

    cmd.Connection.Open()
    reader = cmd.ExecuteReader()
    ddl_cgrade.DataSource = reader
    ddl_cgrade.DataTextField = "Cgrade"
    ddl_cgrade.DataBind()
    reader.Close()
    cmd.Connection.Close()

     

    ashwani

    ashwnai
  • Re: ASP.NET Dropdownlist - "Cannot have multiple items selected in a DropDownList."

    10-15-2007, 12:21 AM

    Any way why do you want to select two items from the list. Can you please give a scenario where this is possible?

    Thanx 

Page 1 of 1 (10 items)