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

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;

In my next article I will discuss on the common misconception in making a particular Dropdownlist value selected.

Cheers and Happy Programming !!!
«May»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910