In my case I had a field on the SQL table populating the dropdownlist set to nvarchar(10) and had omitted to notice that the relevant field on the main data table was char(10).....doh! This meant, of course, that extra spaces were being added to the data
thus rendering the 'selectedvalue' to be non existant. Fixed.
Hey guys, this issue has been plaguing me too. In my code, I have a Gridview populated by a database which will have null values in it. The user then has to edit the data in the database until all cells for a row is filled and then, if the data is good,
the user can accept the record into a primary database (which does not allow null values). Optionally, if the data is bad, the user can reject the record and it will be processed again. In order to make things easier for the user, several of the cells will
become drop down lists when the edit button is pressed. I've had trouble getting my update to recognise the value of a DDL so I followed the guide at http://msdn.microsoft.com/en-gb/library/ms972948.aspx in the hopes that I'll get it right this time.
I've only created one DDL for the time being (may as well get one working before I start on all the others) and it will only have the values "Up" or "Down". These are hard coded. Whenever I hit the "edit" button, I get the error:
"'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value"
I've tried all the methods here and I can't get anything to work. My code for the particular template field is as follows:
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.</div> <div style="position:
absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Parameter name: valu</div>
Edit: Almost forgot, if it helps, the data type for the column is 'nvarchar(4)' and nulls are allowed.
UPDATE:
I read something that suggested that if I add a ListItem at the top of the list like below the error would be solved:
And it did! However, as you will all notice (and as I foolishly didn't notice), when I updated, the value of the cell would become 0 or 1 but I want the value to be Up or Down. Changing Value="0" to Value="Up" and 1 to Down yielded the same error as I have
been receiving all this time.
If you are extracting data from a database that maps to values in your DDL then your item list must have a value for each existing value that is extracted. Are you pre-selecting a value in the list based on the value you read in the database? I don't know
of a way to handle a null value in the list. If you have nulls in the values you are extracting how would they map to a value in your list? Is zero a different value in the database than null? If you want to say that '0' (zero) is that same as null you might
have to convert nulls to zero prior to an edit that tries to pre-select a value of null which will fail.
Any other values in the database that you feel might get read that don't have a value in your item list?
The auto edit in the Gridview will try and pre-select for you based on your bind value.
If you have Visual Studio set a break point at your SQL reader or wherever you are getting data from the database. Then look at the value of the data item in your bind and note whether you are getting a value not in your list item values.
By DDL I'm assuming you mean the Drop Down List. The 'Up' and 'Down' are text strings to be displayed in the drop down list. You have values in of '0' and '1' in the Drop Down List. In your datasource you have nulls, '0' and '1' in a Direction column
of a table of some sort which is what you are binding to. The values in ASP.LISTITEM identified by value='0' etc... is what you are matching on. Try testing with only Direction values of '0' and '1' in your datasource (not the DDL list items) and see if you
get a clean run.
I think there is no doubt that the problem is in the data type of the table field because for example when I make the datatype "text" in ACCESS database I get the error but when I change it to "integer" the problem disappears, but why it works for some "Text"
columns and not for other columns?
Who can say really with these tools. It's open system technology. The same problems in .NET and Java. A lot of time spent type casting and converting values depending on the control or datasource interface. Too much to even try to reason out. I stopped
trying to make sense out of everything in .NET months ago. When things get really layered and problems occur I often peel back the code and get things real simple. Then it is the old fashioned method of looping through adding back code and waiting for the
problem code to rear its ugly head. I also learned to give up on trying to pound a solution into submission. If it is too hard I find another way. Like water around a rock.
I hope this will be the final solution, if somebody has a small project and this solution didn't work with him I hope that he send me his small project so I make sure that he didn't miss any point.
There are two columns involved:
- The one that you retrieve the values from. Let’s call it "Status".
- The one that you insert values to. Let’s call it "process_type".
Suppose you have 3 values in the first column "Status": [New,In progress,Canceled]. In the GridVew or any data control you are showing the second column "process_type", and you want it to be shown as a DropDownList when you click "Edit", then you
have to
open the second column "process_type" from the database and make sure that there is no cell has a value which does not exist in the first column "Status", even Null are not accepted in the second column unless you have null values in the first column.
There is one small thing left you might not notice if you have added a WHERE clause, when you make your SELECT statement for the second column "process_type", and you have added a WHERE clause, you have to make sure that all the values that will be retrieved
from this SELECT statements match all the values that will be retrieved from the first column "Status".
Advice: I think it's obvious now that you have to make the right user interface in such a way to force the user to enter the values that match with the vales from the first column. usually you will be using a dropdownlist.
jharcourt
Member
2 Points
1 Post
Re: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of...
Jul 16, 2009 02:21 PM|LINK
In my case I had a field on the SQL table populating the dropdownlist set to nvarchar(10) and had omitted to notice that the relevant field on the main data table was char(10).....doh! This meant, of course, that extra spaces were being added to the data thus rendering the 'selectedvalue' to be non existant. Fixed.
NobleMule
Member
4 Points
9 Posts
Re: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of...
Jul 28, 2009 11:50 AM|LINK
Hey guys, this issue has been plaguing me too. In my code, I have a Gridview populated by a database which will have null values in it. The user then has to edit the data in the database until all cells for a row is filled and then, if the data is good, the user can accept the record into a primary database (which does not allow null values). Optionally, if the data is bad, the user can reject the record and it will be processed again. In order to make things easier for the user, several of the cells will become drop down lists when the edit button is pressed. I've had trouble getting my update to recognise the value of a DDL so I followed the guide at http://msdn.microsoft.com/en-gb/library/ms972948.aspx in the hopes that I'll get it right this time.
I've only created one DDL for the time being (may as well get one working before I start on all the others) and it will only have the values "Up" or "Down". These are hard coded. Whenever I hit the "edit" button, I get the error:
"'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value"
I've tried all the methods here and I can't get anything to work. My code for the particular template field is as follows:
<asp:TemplateField HeaderText="Direction" SortExpression="Direction"> <EditItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Direction" SelectedValue='<%# Bind("Direction") %>' AppendDataBoundItems="True"> <asp:ListItem Value="0" Selected="True">Up</asp:ListItem> <asp:ListItem Value="1">Down</asp:ListItem> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("Direction") %>'></asp:Label> </ItemTemplate> <ControlStyle BorderColor="#350B59" Width="55px" /> <FooterStyle BorderColor="#350B59" Width="55px" /> <HeaderStyle BorderColor="#350B59" Width="55px" /> <ItemStyle BorderColor="#350B59" Width="55px" /> </asp:TemplateField>Anyone got any ideas? I'm getting desperate!
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Parameter name: valu</div>Edit: Almost forgot, if it helps, the data type for the column is 'nvarchar(4)' and nulls are allowed.
UPDATE:
I read something that suggested that if I add a ListItem at the top of the list like below the error would be solved:
'<asp:ListItem Value="" Selected="True"></asp:ListItem>'
And it did! However, as you will all notice (and as I foolishly didn't notice), when I updated, the value of the cell would become 0 or 1 but I want the value to be Up or Down. Changing Value="0" to Value="Up" and 1 to Down yielded the same error as I have been receiving all this time.
bofcarbon1
Member
53 Points
21 Posts
Re: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of...
Jul 29, 2009 03:57 AM|LINK
Did you get your problem resolved?
If you are extracting data from a database that maps to values in your DDL then your item list must have a value for each existing value that is extracted. Are you pre-selecting a value in the list based on the value you read in the database? I don't know of a way to handle a null value in the list. If you have nulls in the values you are extracting how would they map to a value in your list? Is zero a different value in the database than null? If you want to say that '0' (zero) is that same as null you might have to convert nulls to zero prior to an edit that tries to pre-select a value of null which will fail.
Any other values in the database that you feel might get read that don't have a value in your item list?
The auto edit in the Gridview will try and pre-select for you based on your bind value.
If you have Visual Studio set a break point at your SQL reader or wherever you are getting data from the database. Then look at the value of the data item in your bind and note whether you are getting a value not in your list item values.
B
NobleMule
Member
4 Points
9 Posts
Re: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of...
Jul 29, 2009 09:39 AM|LINK
The DDL is hard coded with values as I only need Up and Down in one list and the months of the year in another. There are no null values in the DDL.
I think that because the DDL is hard coded, the methods to fix this error in this forum aren't working for me.
bofcarbon1
Member
53 Points
21 Posts
Re: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of...
Jul 29, 2009 02:17 PM|LINK
By DDL I'm assuming you mean the Drop Down List. The 'Up' and 'Down' are text strings to be displayed in the drop down list. You have values in of '0' and '1' in the Drop Down List. In your datasource you have nulls, '0' and '1' in a Direction column of a table of some sort which is what you are binding to. The values in ASP.LISTITEM identified by value='0' etc... is what you are matching on. Try testing with only Direction values of '0' and '1' in your datasource (not the DDL list items) and see if you get a clean run.
Muhammad way
Member
22 Points
54 Posts
Re: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of...
Aug 06, 2009 05:42 AM|LINK
Still no solution..
I think there is no doubt that the problem is in the data type of the table field because for example when I make the datatype "text" in ACCESS database I get the error but when I change it to "integer" the problem disappears, but why it works for some "Text" columns and not for other columns?
bofcarbon1
Member
53 Points
21 Posts
Re: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of...
Aug 06, 2009 06:11 AM|LINK
Who can say really with these tools. It's open system technology. The same problems in .NET and Java. A lot of time spent type casting and converting values depending on the control or datasource interface. Too much to even try to reason out. I stopped trying to make sense out of everything in .NET months ago. When things get really layered and problems occur I often peel back the code and get things real simple. Then it is the old fashioned method of looping through adding back code and waiting for the problem code to rear its ugly head. I also learned to give up on trying to pound a solution into submission. If it is too hard I find another way. Like water around a rock.
Muhammad way
Member
22 Points
54 Posts
Re: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of...
Aug 09, 2009 05:07 AM|LINK
I hope this will be the final solution, if somebody has a small project and this solution didn't work with him I hope that he send me his small project so I make sure that he didn't miss any point.
There are two columns involved:
- The one that you retrieve the values from. Let’s call it "Status".
- The one that you insert values to. Let’s call it "process_type".
Suppose you have 3 values in the first column "Status": [New,In progress,Canceled]. In the GridVew or any data control you are showing the second column "process_type", and you want it to be shown as a DropDownList when you click "Edit", then you have to
open the second column "process_type" from the database and make sure that there is no cell has a value which does not exist in the first column "Status", even Null are not accepted in the second column unless you have null values in the first column.
There is one small thing left you might not notice if you have added a WHERE clause, when you make your SELECT statement for the second column "process_type", and you have added a WHERE clause, you have to make sure that all the values that will be retrieved from this SELECT statements match all the values that will be retrieved from the first column "Status".
Advice: I think it's obvious now that you have to make the right user interface in such a way to force the user to enter the values that match with the vales from the first column. usually you will be using a dropdownlist.
Jojan
Member
4 Points
2 Posts
Re: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of...
Aug 11, 2009 11:12 PM|LINK
Please make sure that Datasource has the value you are trying to assign back.
Error happens when control can not find the assigned selected value in the orginal (DataSource) collection.
ArmandoS
Member
20 Points
7 Posts
Re: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of...
Nov 20, 2009 07:36 PM|LINK
Hi,
I solved this issue adding the following to my DropDownList ListItems:
<asp:ListItem></asp:ListItem>
And it worked!
I hope it helps.
Armando
drop down list invalid selected value