"Cannot have multiple items selected in a DropDownList" Error

V

Vi

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
 
P

Phillip Williams

Before you set the selection in a dropdownlist call the clearSelection, e.g.
ddl.ClearSelection();
 
V

Vi

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.
 
P

Phillip Williams

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.
 
V

Vi

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.
 
C

CaffieneRush

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.
 
V

Vi

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
 
C

CaffieneRush

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.
 
A

Adam S

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top