Reputation: 2048
I encounter this problem repeatedly, and haven't a clue what is causing it. I get an exception in the DataBind: SelectedValue which is invalid because it does not exist in the list of items
.
Here are some important pieces of information:
listOrgs.Items.Clear();
listOrgs.SelectedValue = "0";
listOrgs.DataSource = new Organization().DTListAll(SiteID);
listOrgs.DataTextField = "OrganizationName";
listOrgs.DataValueField = "OrganizationID";
listOrgs.DataBind();
Upvotes: 31
Views: 47510
Reputation:
I know its too late to answer, but what I tried is an dirty solution but it worked. After databinding, I am insert a item at index 0
ddl.Items.Insert(0, new ListItem("---Select---","-1"));
And on setting,
I am placing try catch, In catch i am setting Value to -1
Upvotes: 0
Reputation: 11
Not sure it is your case, but I had the same problem and apparently there was no explanation, then I realized doing a copy and paste on notepad of a field of database that at the beginning of the value there was a NULL.
Curious thing was that a select joining tables was working. I deleted the row and reinserted, after was working fine.
Upvotes: 1
Reputation: 1
I was getting the same error repeatedly and try ending up by not setting the default selected value to Index -1.
I commented my code ddlDRIBidAmt.SelectedValue = -1
This value was set at the time where my Page Controls were reset to default values.
Upvotes: 0
Reputation: 3630
I kept getting this error.
Weird thing is that before I set the datasource and rebind after deleting an item the selected index = -1.
If I explicitly set the selectedIndex = -1;
then it works and doesn't throw an error.
So even though it was already -1 setting it to -1 stops it from erroring.
Weird eh?
Upvotes: 8
Reputation: 17101
@PMarques answer helped me out and did solve my problem.
However whilst experimenting, it clicked in my head why I was gettign the error in the first place.
I was setting the "Text" attribute thinking that it might create an encompassing label or fieldset + legend, for me (which it doesn't).
The Text property for a list is infact the SelectedValue property for a ListControl.
So my mistake in misinterpreting what the text property did.
Upvotes: 1
Reputation: 601
Apparently the solution I posted wasn't entirely effective... Eventually in my application I changed to this:
listOrgs.Items.Clear();
listOrgs.SelectedIndex = -1;
listOrgs.SelectedValue = null;
listOrgs.ClearSelection(); // Clears the selection to avoid the exception (only one of these should be enough but in my application I needed all..)
listOrgs.DataSource = new Organization().DTListAll(SiteID);
listOrgs.DataTextField = "OrganizationName";
listOrgs.DataValueField = "OrganizationID";
listOrgs.DataBind();
Upvotes: 60
Reputation: 19
In case you still have this problem this is how i resolved it:
listOrgs.SelectedIndex = -1; // Clears the SelectedIndex to avoid the exception
listOrgs.DataSource = new Organization().DTListAll(SiteID);
listOrgs.DataTextField = "OrganizationName";
listOrgs.DataValueField = "OrganizationID";
listOrgs.DataBind(); //Unless you have "listOrgs.AppendDataBoundItems = true" you don't need to clear the list
Upvotes: 1
Reputation: 137148
Try setting listOrgs.SelectedValue = "0"
after you refresh the DataSource
At the moment you are trying to select the first item in an empty list.
Upvotes: 3
Reputation: 48088
Change the first two line with this :
listOrgs.SelectedItem.Selected = false;
listOrgs.Items.Clear();
Upvotes: 1