Bob Jones
Bob Jones

Reputation: 2048

SelectedValue which is invalid because it does not exist in the list of items

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:

  1. I reload listOrgs periodically when the underlying data has changed.
  2. The Organization.DTListAll call returns 2 Int, String pairs.
  3. There are no duplicate or null values in the returned data
  4. After the first two lines below, listOrgs.Items.Count is 0, and the Selected Value is 0
  5. The selected value after the DataBind operation executes is the ID value from the first row in the data
  6. This exception happens the very first time this code is executed after a fresh page load
listOrgs.Items.Clear(); 
listOrgs.SelectedValue = "0"; 
listOrgs.DataSource = new Organization().DTListAll(SiteID); 
listOrgs.DataTextField = "OrganizationName"; 
listOrgs.DataValueField = "OrganizationID"; 
listOrgs.DataBind();

Upvotes: 31

Views: 47510

Answers (9)

user240141
user240141

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

Alex
Alex

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

Anjali
Anjali

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

DazManCat
DazManCat

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

Alex KeySmith
Alex KeySmith

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

PMarques
PMarques

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

PMarques
PMarques

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

ChrisF
ChrisF

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

Canavar
Canavar

Reputation: 48088

Change the first two line with this :

listOrgs.SelectedItem.Selected = false; 
listOrgs.Items.Clear(); 

Upvotes: 1

Related Questions