I have two dropdownlist and a button. I used the breakpoint in my project and everything is working fine. But as soon I am getting out of the function of the button this is
I was trying to add two other list items to to top of the DropDownList
's list after reading data into the DropDownList
.
One of the item was "please pick one...", and the second one was "Not listed here...". So I have created a list item:
ListItem li1 = new ListItem("please pick one...", "999");
ListItem li2 = new ListItem("not listed here...", "555");
Then I have tried to add these two ListItems
to the three DropDownList
. After that I have encountered the same error.
After creating new ListItem instances for each DropDownList
, the problem has gone away...
Found another way to get the error:
ddlFromBudget.Items.Clear();
ListItem newItem = new ListItem();
newItem.Text = "Not Set";
newItem.Value = "0";
ddlFromBudget.Items.Add(newItem);
if (ddlB1.SelectedValue.ToString() != "0")
{
newItem = new ListItem();
newItem.Text = ddlB1.SelectedItem.ToString();
newItem.Value = "1";
ddlFromBudget.Items.Add(newItem);
}
The line ddlFromBudget.Items.Add(newItem);
sets newItem.Selected = True
.
Without the line newItem = new ListItem();
, you get the error because the selected flag is now true on both items added to the ddl.