SelectedValue which is invalid because it does not exist in the list of items - asp.net

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:
I reload listOrgs periodically when the underlying data has changed.
The Organization.DTListAll call returns 2 Int, String pairs.
There are no duplicate or null values in the returned data
After the first two lines below, listOrgs.Items.Count is 0, and the Selected Value is 0
The selected value after the DataBind operation executes is the ID value from the first row in the data
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();

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();

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?

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.

Change the first two line with this :
listOrgs.SelectedItem.Selected = false;
listOrgs.Items.Clear();

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

#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.

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.

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.

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

Related

How to remove duplicates in a listbox VB.net

I am trying to remove duplicates in a ListBox which is populated by a query pull. I use this code to prevent adding duplicates in VB 6.0 but does not work when converted over to VB.net. Is there a substitute method to prevent or remove duplicates.
colSchema = dr("Col_Schema").ToString
If Not lstSchema.Items.ToString.Contains(colSchema) Then
lstSchema.Items.Add(New ListItem(colSchema))
End If
try
colSchema = dr("Col_Schema").ToString
dim exists as boolean = false
for i as integer = 0 to lstSchema.items.count - 1
if lstSchema.items.item(i) = colSchema then
exists = true
end if
next
if exists = false then
lstSchema.Items.Add(New ListItem(colSchema))
end if
This code
lstSchema.Items.ToString
is converting Items to a string. Items is most likely the type ListBox.ObjectCollection (if this is WinForms) or a similar collection type for other UI frameworks. Calling ToString on such classes will end up calling Object.ToString, which just returns the name of the class.
Instead, try
lstSchema.Items.Contains(colSchema)
If that does not work for some reason, please update your question explaining exactly what you were trying to solve by calling ToString.

Cant get ExtededDataGrid in Flex to filter with ComboBox on multiple columns

LATEST UPDATE: Issue answered here. Some one else at stackoverflow had a similar issue and it was resolved. Solution provided for convenience. This is the line of code I was missing:
comboHeaderColumn.useLabelFunctionForFilterCompare = true;
that line is followed by these:
comboHeaderColumn.filterComboBoxBuildFromGrid = true;
comboHeaderColumn.labelFunction = formatState;
where formatState is a local method that formats the data for the combobox.
UPDATE: I've now got the combobox's loading with the correct data, but when I select a value nothing happens. The combo boxes load only data that is in the column, and when you select a value in the combobox, it's supposed to filter the rows on that value. It doesn't.
Thanks for looking. I'm having trouble getting multiple filters to work in Flex in Flash Builder 4 using the ExtendedDataGrid and ComboBox's. Here is an image of part of the grid:
The User Name and City filter properly if you type text into the box's above the column header and the Request Date lets you select date ranges if you click on the Custom bar, but the Request Reason and State ComboBoxes do not list anything. I've created them using comboHeaderColumn.filterComboBoxBuildFromGrid = true; but all it does is put "[object Object]" as the only other selection under All.
I've used this article but it will only allow you to use a single filter for the entire grid.
My finished grid will have about 20 columns and from 20,000 to 450,000 rows of data so the filters are really important and I'll need more than one.
The code is very straight forward and loops through all the returned data and if the column is identified as a filter column it does this:
comboHeaderColumn.filterComboBoxDataProvider = codeValuePairs;
comboHeaderColumn.filterComboBoxLabelField = "Value";
comboHeaderColumn.filterControl = "ComboBox";
comboHeaderColumn.filterOperation = FilterExpression.FILTER_OPERATION_TYPE_EQUALS;
comboHeaderColumn.headerText = ac.Header;
comboHeaderColumn.dataField = ac.Name;
if( ac.Header == "State" || ac.Header == "Request Reason" )
{
comboHeaderColumn.filterComboBoxBuildFromGrid = true;
}
ProfileDataColumns.push(comboHeaderColumn);
This creates 2 entries in the combo box: All and [object Object]
What am I missing??? Anyway, after half a day searching I decided to reach out.
Any suggestions or direction to an article would be very much appreciated.
Thanks.

Why would DropDownList.Text is return 0 when it is clearly an empty string?

I have a drop down list that I am binding to a record that clearly sets value=0, text=''
the dropdownlist looks fine, i do not see any text in the control BUT when i try to validate in the code behind it says the dropdownlist.text = 0
i do not know why the control shows no text on the form but holds a value in its .text property, it doesn't make any sense. can anyone help? Thanks.
I fixed the issue I was having by inserting a null value along with the text of '' and now the dropdownlist.text is = '' intead of 0.

Unable to set value of leading in TextInput

I need to set the value of leading of text layer in Photoshop. I am first retrieving the selected text layer and then gets its value in a TextItem object. Then I get the value of the leading from a combo-box and set the value of leading using the following code.
var activeTextItem:TextItem = curLayer.textItem;
activeTextItem.leading = ComboBox.text.toString();
This code works fine when using on Windows. But when I try to execute the above code on Mac it always shows the leading as null object.
Can someone please guide me why I am not able to set the value of leading in Mac?
Thanks
I found the solution for this.
The leading has a value called Auto which by default has value a null. Therefore when I use the above code I am not able to set the value as leading parameter is null.
To overcome this I put a check that if leading is null value ie Auto then set the property useAutoLeading to false and once you set the value then again set the useAutoLeading property to true.
if("Auto" == ComboBox.text)
{
activeTextItem.useAutoLeading = false;
activeTextItem.leading = ComboBox.text.valueOf();
activeTextItem.useAutoLeading = true;
}
Thanks.

Object Data Source

I'm creating a gridview using an objectdatasource and it works fine when pulling all records. But when I want to use the selectCountMethod the grid shows no values.
I Step through the code and my getInvoices (gets the requested data) returns data and the getInvoicesCount (gets the total record count). But then when I go through the rowdatabound of the gridview there's nothing in there and no data displays.
Here is my code to set the objectdatasource. Any reasons why it wouldn't work or something special that needs to be done for getting the selectcount to work?
Me.ODS.TypeName = "invoice"
Me.ODS.EnablePaging = True
Me.ODS.SelectMethod = "getInvoices"
Me.ODS.SelectCountMethod = "GetInvoiceCount"
Me.ODS.StartRowIndexParameterName = "startRowIndex"
Me.ODS.MaximumRowsParameterName = "maximumRows"
Me.ODS.SelectParameters.Add("strbu", strBusUnit)
Me.ODS.SelectParameters.Add("stremailAddress", emailAddress)
Me.ODS.SelectParameters.Add("startDate", search_startdate)
Me.ODS.SelectParameters.Add("enddate", search_enddate)
Me.ODS.SelectParameters.Add("sortExpression", sortExpression & " " & sortDirection)
With gvInvoices
.PageIndex = intPageIndex
.PageSize = 25
.DataBind()
End With
Check if the count being returned is an integer . debug it . maybe it is null.
and if not null parse it to an integer
I was able to figure this one out. The count was being returned as a long instead of integer. I changed it to integer and all is working great

Resources