"Cannot have multiple items selected in a DropDownList" Error

Discussion in 'ASP .Net Web Controls' started by Vi, Apr 4, 2006.

  1. Vi

    Vi Guest

    Hi,
    I'm getting the error "Cannot have multiple items selected in a
    DropDownList" when I'm trying to select a specific value in a
    System.Web.UI.WebControls.DropDownList by using the code:
    ddl.SelectedValue = "3";

    I'm not setting it to any other value anywhere in the code. All I'm doing
    with that list is populating it by DataBind-ing it to a SqlReader and then
    adding an additional ListItem to it:

    ddl.Items.Insert(0, oItem);

    I guess ddl.Items.Insert() also selects the inserted value, since if I
    comment out this line I don't get the error anymore.

    Does anybody know how to fix this or should I just create some client side
    code that will run after the page loads and will make all the selectios?

    Thanks
     
    Vi, Apr 4, 2006
    #1
    1. Advertising

  2. Before you set the selection in a dropdownlist call the clearSelection, e.g.
    ddl.ClearSelection();
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Vi" wrote:

    > Hi,
    > I'm getting the error "Cannot have multiple items selected in a
    > DropDownList" when I'm trying to select a specific value in a
    > System.Web.UI.WebControls.DropDownList by using the code:
    > ddl.SelectedValue = "3";
    >
    > I'm not setting it to any other value anywhere in the code. All I'm doing
    > with that list is populating it by DataBind-ing it to a SqlReader and then
    > adding an additional ListItem to it:
    >
    > ddl.Items.Insert(0, oItem);
    >
    > I guess ddl.Items.Insert() also selects the inserted value, since if I
    > comment out this line I don't get the error anymore.
    >
    > Does anybody know how to fix this or should I just create some client side
    > code that will run after the page loads and will make all the selectios?
    >
    > Thanks
     
    Phillip Williams, Apr 4, 2006
    #2
    1. Advertising

  3. Vi

    Vi Guest

    Thanls for your reply, Philip, but it did not work - I get the same error.
    This is what I did:
    lstWhatBrings.ClearSelection();
    lstWhatBrings.SelectedValue = "2";

    And I still get "Cannot have multiple items selected in a DropDownList"
    Thanks again.


    "Phillip Williams" wrote:

    > Before you set the selection in a dropdownlist call the clearSelection, e.g.
    > ddl.ClearSelection();
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Vi" wrote:
    >
    > > Hi,
    > > I'm getting the error "Cannot have multiple items selected in a
    > > DropDownList" when I'm trying to select a specific value in a
    > > System.Web.UI.WebControls.DropDownList by using the code:
    > > ddl.SelectedValue = "3";
    > >
    > > I'm not setting it to any other value anywhere in the code. All I'm doing
    > > with that list is populating it by DataBind-ing it to a SqlReader and then
    > > adding an additional ListItem to it:
    > >
    > > ddl.Items.Insert(0, oItem);
    > >
    > > I guess ddl.Items.Insert() also selects the inserted value, since if I
    > > comment out this line I don't get the error anymore.
    > >
    > > Does anybody know how to fix this or should I just create some client side
    > > code that will run after the page loads and will make all the selectios?
    > >
    > > Thanks
     
    Vi, Apr 4, 2006
    #3
  4. Well, the short answer is that it should not be happening. If you have not
    found the cause yet; post the code (including that for databinding).
    Something in your code causes the problem.
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Vi" wrote:

    > Thanls for your reply, Philip, but it did not work - I get the same error.
    > This is what I did:
    > lstWhatBrings.ClearSelection();
    > lstWhatBrings.SelectedValue = "2";
    >
    > And I still get "Cannot have multiple items selected in a DropDownList"
    > Thanks again.
    >
    >
    > "Phillip Williams" wrote:
    >
    > > Before you set the selection in a dropdownlist call the clearSelection, e.g.
    > > ddl.ClearSelection();
    > > --
    > > HTH,
    > > Phillip Williams
    > > http://www.societopia.net
    > > http://www.webswapp.com
    > >
    > >
    > > "Vi" wrote:
    > >
    > > > Hi,
    > > > I'm getting the error "Cannot have multiple items selected in a
    > > > DropDownList" when I'm trying to select a specific value in a
    > > > System.Web.UI.WebControls.DropDownList by using the code:
    > > > ddl.SelectedValue = "3";
    > > >
    > > > I'm not setting it to any other value anywhere in the code. All I'm doing
    > > > with that list is populating it by DataBind-ing it to a SqlReader and then
    > > > adding an additional ListItem to it:
    > > >
    > > > ddl.Items.Insert(0, oItem);
    > > >
    > > > I guess ddl.Items.Insert() also selects the inserted value, since if I
    > > > comment out this line I don't get the error anymore.
    > > >
    > > > Does anybody know how to fix this or should I just create some client side
    > > > code that will run after the page loads and will make all the selectios?
    > > >
    > > > Thanks
     
    Phillip Williams, Apr 4, 2006
    #4
  5. Vi

    Vi Guest

    Here's the code:
    if(!IsPostBack)
    {
    System.Web.UI.WebControls.ListItem oItem = new
    System.Web.UI.WebControls.ListItem();
    oItem.Value = "0";
    oItem.Text = "-- Please Select --";
    dr = slDatabase.ExecuteReader1("sp_get_all_products");
    ddl.DataSource = dr;
    ddl.DataTextField = "product_name";
    ddl.DataValueField = "product_id";
    ddl.DataBind();
    ddl.Items.Insert(0, oItem);
    ////ddl.SelectedValue = "0";
    dr.Close();

    ddl.ClearSelection();
    ddl.SelectedIndex = 1;
    //// I also tried ddl.SelectedValue = "1"; but I get the same error
    }

    thanks again, Phillip.


    "Phillip Williams" wrote:

    > Well, the short answer is that it should not be happening. If you have not
    > found the cause yet; post the code (including that for databinding).
    > Something in your code causes the problem.
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Vi" wrote:
    >
    > > Thanls for your reply, Philip, but it did not work - I get the same error.
    > > This is what I did:
    > > lstWhatBrings.ClearSelection();
    > > lstWhatBrings.SelectedValue = "2";
    > >
    > > And I still get "Cannot have multiple items selected in a DropDownList"
    > > Thanks again.
    > >
    > >
    > > "Phillip Williams" wrote:
    > >
    > > > Before you set the selection in a dropdownlist call the clearSelection, e.g.
    > > > ddl.ClearSelection();
    > > > --
    > > > HTH,
    > > > Phillip Williams
    > > > http://www.societopia.net
    > > > http://www.webswapp.com
    > > >
    > > >
    > > > "Vi" wrote:
    > > >
    > > > > Hi,
    > > > > I'm getting the error "Cannot have multiple items selected in a
    > > > > DropDownList" when I'm trying to select a specific value in a
    > > > > System.Web.UI.WebControls.DropDownList by using the code:
    > > > > ddl.SelectedValue = "3";
    > > > >
    > > > > I'm not setting it to any other value anywhere in the code. All I'm doing
    > > > > with that list is populating it by DataBind-ing it to a SqlReader and then
    > > > > adding an additional ListItem to it:
    > > > >
    > > > > ddl.Items.Insert(0, oItem);
    > > > >
    > > > > I guess ddl.Items.Insert() also selects the inserted value, since if I
    > > > > comment out this line I don't get the error anymore.
    > > > >
    > > > > Does anybody know how to fix this or should I just create some client side
    > > > > code that will run after the page loads and will make all the selectios?
    > > > >
    > > > > Thanks
     
    Vi, Apr 4, 2006
    #5
  6. I ran this code without getting any error!
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Vi" wrote:

    > Here's the code:
    > if(!IsPostBack)
    > {
    > System.Web.UI.WebControls.ListItem oItem = new
    > System.Web.UI.WebControls.ListItem();
    > oItem.Value = "0";
    > oItem.Text = "-- Please Select --";
    > dr = slDatabase.ExecuteReader1("sp_get_all_products");
    > ddl.DataSource = dr;
    > ddl.DataTextField = "product_name";
    > ddl.DataValueField = "product_id";
    > ddl.DataBind();
    > ddl.Items.Insert(0, oItem);
    > ////ddl.SelectedValue = "0";
    > dr.Close();
    >
    > ddl.ClearSelection();
    > ddl.SelectedIndex = 1;
    > //// I also tried ddl.SelectedValue = "1"; but I get the same error
    > }
    >
    > thanks again, Phillip.
    >
    >
    > "Phillip Williams" wrote:
    >
    > > Well, the short answer is that it should not be happening. If you have not
    > > found the cause yet; post the code (including that for databinding).
    > > Something in your code causes the problem.
    > > --
    > > HTH,
    > > Phillip Williams
    > > http://www.societopia.net
    > > http://www.webswapp.com
    > >
    > >
    > > "Vi" wrote:
    > >
    > > > Thanls for your reply, Philip, but it did not work - I get the same error.
    > > > This is what I did:
    > > > lstWhatBrings.ClearSelection();
    > > > lstWhatBrings.SelectedValue = "2";
    > > >
    > > > And I still get "Cannot have multiple items selected in a DropDownList"
    > > > Thanks again.
    > > >
    > > >
    > > > "Phillip Williams" wrote:
    > > >
    > > > > Before you set the selection in a dropdownlist call the clearSelection, e.g.
    > > > > ddl.ClearSelection();
    > > > > --
    > > > > HTH,
    > > > > Phillip Williams
    > > > > http://www.societopia.net
    > > > > http://www.webswapp.com
    > > > >
    > > > >
    > > > > "Vi" wrote:
    > > > >
    > > > > > Hi,
    > > > > > I'm getting the error "Cannot have multiple items selected in a
    > > > > > DropDownList" when I'm trying to select a specific value in a
    > > > > > System.Web.UI.WebControls.DropDownList by using the code:
    > > > > > ddl.SelectedValue = "3";
    > > > > >
    > > > > > I'm not setting it to any other value anywhere in the code. All I'm doing
    > > > > > with that list is populating it by DataBind-ing it to a SqlReader and then
    > > > > > adding an additional ListItem to it:
    > > > > >
    > > > > > ddl.Items.Insert(0, oItem);
    > > > > >
    > > > > > I guess ddl.Items.Insert() also selects the inserted value, since if I
    > > > > > comment out this line I don't get the error anymore.
    > > > > >
    > > > > > Does anybody know how to fix this or should I just create some client side
    > > > > > code that will run after the page loads and will make all the selectios?
    > > > > >
    > > > > > Thanks
     
    Phillip Williams, Apr 4, 2006
    #6
  7. me too :)

    "Phillip Williams" <> wrote in message
    news:...
    >I ran this code without getting any error!
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Vi" wrote:
    >
    >> Here's the code:
    >> if(!IsPostBack)
    >> {
    >> System.Web.UI.WebControls.ListItem oItem = new
    >> System.Web.UI.WebControls.ListItem();
    >> oItem.Value = "0";
    >> oItem.Text = "-- Please Select --";
    >> dr = slDatabase.ExecuteReader1("sp_get_all_products");
    >> ddl.DataSource = dr;
    >> ddl.DataTextField = "product_name";
    >> ddl.DataValueField = "product_id";
    >> ddl.DataBind();
    >> ddl.Items.Insert(0, oItem);
    >> ////ddl.SelectedValue = "0";
    >> dr.Close();
    >>
    >> ddl.ClearSelection();
    >> ddl.SelectedIndex = 1;
    >> //// I also tried ddl.SelectedValue = "1"; but I get the same error
    >> }
    >>
    >> thanks again, Phillip.
    >>
    >>
    >> "Phillip Williams" wrote:
    >>
    >> > Well, the short answer is that it should not be happening. If you have
    >> > not
    >> > found the cause yet; post the code (including that for databinding).
    >> > Something in your code causes the problem.
    >> > --
    >> > HTH,
    >> > Phillip Williams
    >> > http://www.societopia.net
    >> > http://www.webswapp.com
    >> >
    >> >
    >> > "Vi" wrote:
    >> >
    >> > > Thanls for your reply, Philip, but it did not work - I get the same
    >> > > error.
    >> > > This is what I did:
    >> > > lstWhatBrings.ClearSelection();
    >> > > lstWhatBrings.SelectedValue = "2";
    >> > >
    >> > > And I still get "Cannot have multiple items selected in a
    >> > > DropDownList"
    >> > > Thanks again.
    >> > >
    >> > >
    >> > > "Phillip Williams" wrote:
    >> > >
    >> > > > Before you set the selection in a dropdownlist call the
    >> > > > clearSelection, e.g.
    >> > > > ddl.ClearSelection();
    >> > > > --
    >> > > > HTH,
    >> > > > Phillip Williams
    >> > > > http://www.societopia.net
    >> > > > http://www.webswapp.com
    >> > > >
    >> > > >
    >> > > > "Vi" wrote:
    >> > > >
    >> > > > > Hi,
    >> > > > > I'm getting the error "Cannot have multiple items selected in a
    >> > > > > DropDownList" when I'm trying to select a specific value in a
    >> > > > > System.Web.UI.WebControls.DropDownList by using the code:
    >> > > > > ddl.SelectedValue = "3";
    >> > > > >
    >> > > > > I'm not setting it to any other value anywhere in the code. All
    >> > > > > I'm doing
    >> > > > > with that list is populating it by DataBind-ing it to a SqlReader
    >> > > > > and then
    >> > > > > adding an additional ListItem to it:
    >> > > > >
    >> > > > > ddl.Items.Insert(0, oItem);
    >> > > > >
    >> > > > > I guess ddl.Items.Insert() also selects the inserted value, since
    >> > > > > if I
    >> > > > > comment out this line I don't get the error anymore.
    >> > > > >
    >> > > > > Does anybody know how to fix this or should I just create some
    >> > > > > client side
    >> > > > > code that will run after the page loads and will make all the
    >> > > > > selectios?
    >> > > > >
    >> > > > > Thanks
     
    Algimantas Krasauskas, Apr 4, 2006
    #7
  8. Vi

    Guest

    I set DropDownList.SelectedIndex to select a specific item in the
    dropdownlist rather than set DropDownList.SelectedValue.

    eg.
    'Select the first Item in DropDownList.
    ddl.SelectedIndex = 0

    Hope that helps.
     
    , Apr 4, 2006
    #8
  9. Vi

    Vi Guest

    you're right, the sample code I send does work. I should've sent a bigger
    part of my code as sample, because I have several DropDowns on the same page
    and the error is generated when I add the common ListItem object to the
    second DropDown. Here's the code:
    if(!IsPostBack)
    {
    System.Web.UI.WebControls.ListItem oItem = new
    System.Web.UI.WebControls.ListItem();
    oItem.Value = "0";
    oItem.Text = "-- Please Select --";

    dr = slDatabase.ExecuteReader1("sp_get_all_products");
    ddl.DataSource = dr;
    ddl.DataTextField = "product_name";
    ddl.DataValueField = "product_id";
    ddl.DataBind();
    ddl.Items.Insert(0, oItem);
    ////ddl.SelectedValue = "0";
    dr.Close();

    ddl.ClearSelection();
    ddl.SelectedIndex = 1;

    dr = slDatabase.ExecuteReader1("sp_get_all_companies");
    ddl2.DataSource = dr;
    ddl2.DataTextField = "company_name";
    ddl2.DataValueField = "company_id";
    ddl2.DataBind();
    //// ddl2.Items.Insert(0, oItem); -- THIS LINE GENERATES THE ERROR
    }

    I'm still not sure why this lines generates "Cannot have multiple items
    selected in a DropDownList" error, since I'm not making any selections here.
    Thanks


    "Phillip Williams" wrote:

    > I ran this code without getting any error!
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Vi" wrote:
    >
    > > Here's the code:
    > > if(!IsPostBack)
    > > {
    > > System.Web.UI.WebControls.ListItem oItem = new
    > > System.Web.UI.WebControls.ListItem();
    > > oItem.Value = "0";
    > > oItem.Text = "-- Please Select --";
    > > dr = slDatabase.ExecuteReader1("sp_get_all_products");
    > > ddl.DataSource = dr;
    > > ddl.DataTextField = "product_name";
    > > ddl.DataValueField = "product_id";
    > > ddl.DataBind();
    > > ddl.Items.Insert(0, oItem);
    > > ////ddl.SelectedValue = "0";
    > > dr.Close();
    > >
    > > ddl.ClearSelection();
    > > ddl.SelectedIndex = 1;
    > > //// I also tried ddl.SelectedValue = "1"; but I get the same error
    > > }
    > >
    > > thanks again, Phillip.
    > >
    > >
    > > "Phillip Williams" wrote:
    > >
    > > > Well, the short answer is that it should not be happening. If you have not
    > > > found the cause yet; post the code (including that for databinding).
    > > > Something in your code causes the problem.
    > > > --
    > > > HTH,
    > > > Phillip Williams
    > > > http://www.societopia.net
    > > > http://www.webswapp.com
    > > >
    > > >
    > > > "Vi" wrote:
    > > >
    > > > > Thanls for your reply, Philip, but it did not work - I get the same error.
    > > > > This is what I did:
    > > > > lstWhatBrings.ClearSelection();
    > > > > lstWhatBrings.SelectedValue = "2";
    > > > >
    > > > > And I still get "Cannot have multiple items selected in a DropDownList"
    > > > > Thanks again.
    > > > >
    > > > >
    > > > > "Phillip Williams" wrote:
    > > > >
    > > > > > Before you set the selection in a dropdownlist call the clearSelection, e.g.
    > > > > > ddl.ClearSelection();
    > > > > > --
    > > > > > HTH,
    > > > > > Phillip Williams
    > > > > > http://www.societopia.net
    > > > > > http://www.webswapp.com
    > > > > >
    > > > > >
    > > > > > "Vi" wrote:
    > > > > >
    > > > > > > Hi,
    > > > > > > I'm getting the error "Cannot have multiple items selected in a
    > > > > > > DropDownList" when I'm trying to select a specific value in a
    > > > > > > System.Web.UI.WebControls.DropDownList by using the code:
    > > > > > > ddl.SelectedValue = "3";
    > > > > > >
    > > > > > > I'm not setting it to any other value anywhere in the code. All I'm doing
    > > > > > > with that list is populating it by DataBind-ing it to a SqlReader and then
    > > > > > > adding an additional ListItem to it:
    > > > > > >
    > > > > > > ddl.Items.Insert(0, oItem);
    > > > > > >
    > > > > > > I guess ddl.Items.Insert() also selects the inserted value, since if I
    > > > > > > comment out this line I don't get the error anymore.
    > > > > > >
    > > > > > > Does anybody know how to fix this or should I just create some client side
    > > > > > > code that will run after the page loads and will make all the selectios?
    > > > > > >
    > > > > > > Thanks
     
    Vi, Apr 4, 2006
    #9
  10. Vi

    Guest

    Sorry, I did not see that others have posted the same suggestion
    already.
     
    , Apr 4, 2006
    #10
  11. Vi

    Guest

    I wonder if you can add the same ListItem to several DropDownLists.

    Try the following to see if your exception goes away:
    ddl.Items.Insert(0, new ListItem("-- Please Select --", 0);
    ddl.SelectedIndex = 0;
    '...
    ddl2.Items.Insert(0, new ListItem("-- Please Select --", 0);
    ddl2.SelectedIndex = 0;

    Regards.
     
    , Apr 4, 2006
    #11
  12. Vi

    Adam S Guest

    I've seen this error.
    I realise that this thread is old, but perhaps you still need a
    solution.

    I've found that inserting/adding items to a DropDown can create problems
    (especially inserting a ListItem after performing a Databind!).

    To get around this, I retrieve the database info I'm using for the
    DropDown into a DataTable.

    Then I insert the Default entry into the DataTable at the 0 index.

    Then, bind the DataTable to your DropDown.

    This has helped me resolve a lot of weird errors.

    e.g.,

    Dim dt as DataTable = (some routine that returns a datatable)

    Dim dr As DataRow = dt.NewRow
    dr(0) = 0
    dr(1) = "-- Please Select --"
    dt.Rows.InsertAt(dr, 0)

    dd1.DataSource = dtDD
    dd1.DataTextField = "Text"
    dd1.DataValueField = "Value"
    dd1.DataBind()



    *** Sent via Developersdex http://www.developersdex.com ***
     
    Adam S, Dec 20, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hfk0
    Replies:
    2
    Views:
    21,675
  2. JavaQueries
    Replies:
    1
    Views:
    3,675
    John C. Bollinger
    Mar 1, 2005
  3. Balaji
    Replies:
    3
    Views:
    10,115
  4. Bishop
    Replies:
    1
    Views:
    791
    Bishop
    Feb 24, 2007
  5. =?Utf-8?B?S3Jpc2huYQ==?=

    ERROR: Cannot have multiple items selected in a DropDownList.

    =?Utf-8?B?S3Jpc2huYQ==?=, Jun 25, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    612
    Eliyahu Goldin
    Jun 25, 2007
Loading...

Share This Page