Reputation: 307
I have two DropDownLists and I'm adding Items like this:
using (SqlDataReader reader = command.ExecuteReader())
{
ddUserTarget.Items.Add(new ListItem(" ", "empty534534545"));
while (reader.Read())
{
ddUsersSource.Items.Add(new ListItem(reader[1].ToString(), reader[0].ToString()));
ddUserTarget.Items.Add(new ListItem(reader[1].ToString(), reader[0].ToString()));
}
}
When I'm use one of this DropDownList after a button click or a autopostback there are this HttpException:
In the DropDownList it's not allowed to select more than one item. (0x80004005)
If I delete this line, the error will not appear:
ddUserTarget.Items.Add(new ListItem(" ","empty534534545"));
The error is triggered at this position:
else if (ddUserTarget.SelectedValue == "test")
I really dont know what to do now....
Upvotes: 0
Views: 243
Reputation: 6590
Try this. In while loop you are inserting same Items twice.
using (SqlDataReader reader = command.ExecuteReader())
{
ddUserTarget.Items.Insert(0,new ListItem("empty534534545"));
while (reader.Read())
{
ddUsersSource.Items.Insert(ddUsersSource.Items.Count,new ListItem(reader[1].ToString(), reader[0].ToString()));
}
}
Upvotes: 0
Reputation: 645
Probably you are setting a value to "SelectedItem" in more than one place. I suggest you clear the selected items before post back (at the page end). for example:
DropDownList myDDL = new DropDownList();
myDDL.ClearSelection();
Upvotes: 2