'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.

I have researched this problem, and the answers I seem to be getting have more to do with a more complex solution. This cant be that hard...

- I have a gridview bound to a SQL table
- I converted a column in the gridview to a template
- I edited the template and added a dropdown to the "edit item view"
- I bound the dropdown to a new datasource object that does nothing more than return all the ID and a NAME fields. (no fancy SQL here)
- I chose databinding and selected "selected value" from the left "bindable properties" box
- I chose the field I was binding to from the "bind to" drop down box on the right side
- two way databinding is selected

when i run it i get this error..
'DropDownList1' has a Selected Value which is invalid because it does not exist in the list of items.
Parameter name: value

Several solutions i have seen have you editing the "on-change" event and writing code to do what should be a very simple operation. Other solutions i have seen do a complete walkthrough on this and say that it works perfectly, while others that have followed these walkthroughs (myself included) say that they are getting this same error. There must be some setting or something that some people have set that they are not thinking about that allows this to work for some people and not for others.

Any Ideas, please?

Thank you
gigasausageAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Bob LearnedCommented:
Are you binding both the display field and the value for the DropDownList?  If you are, are the values unique within the list?

Bob
0
 
gigasausageAuthor Commented:
the problem exists when you try to edit a record in which the bound field is Null. since thre is no Null field in the dropdown dataset it throws this error.
0
 
Bob LearnedCommented:
So, the ID field isn't a primary key?  Are you are talking about the Name property?  

Bob
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gigasausageAuthor Commented:
im thinking something like this should happen automatically somewhere (pardon the psudo code)

if BoundColumn.value = null then
     dropdown.selecteditem = "<select value from dropdown...>"
else
     dropdown.selecteditem = BoundColumn.value
end if
0
 
gigasausageAuthor Commented:
the ID field in the dataset bound to the gridview is not a primary key in the table for which the value is stored. It is a primary key for the table in which the dropdown is bound.
0
 
Bob LearnedCommented:
I am trying to understand the problem space.

   1) You have an existing record?
   2) You are adding a new record?
   3) Does the data source for the DropDownList have an entry to handle null text (i.e. (None))?

Bob
0
 
gigasausageAuthor Commented:
<table 1> - bound to Gridview
LotID -(Primary Key)
LotDescription
BuilderId

<table 2> - bound to dropdown
builderID -(Primary Key)
buildername

<Table 1 data>
1, blah blah blah, null
2, some lot description, null

<Table 2 data>
1, builder name 1
2, buildername 2


scenario...
1) open gridview and see table 1 data
2) click edit
3) get error because dropdown cant select Null from its datasource
0
 
gigasausageAuthor Commented:
The obvious easy answer would be not to allow nulls to be entered in table 1 except that is not realisticly available. In this and many other applications you will need to fill in a blank form, for which the current value will be null.
0
 
Bob LearnedCommented:
>>I bound the dropdown to a new datasource object that does nothing more than return all the ID and a NAME fields. (no fancy SQL here)
Can you show me the declaration on this one, please?

Bob
0
 
gigasausageAuthor Commented:
<EditItemTemplate>

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
        DataTextField="BuilderName" DataValueField="BuilderID" OnLoad="DropDownList1_Load"
        SelectedValue='<%# Bind("BuilderID") %>'>
</asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT [BuilderID], [BuilderName] FROM [tbl_Builders] ">
</asp:SqlDataSource>

</EditItemTemplate>
0
 
Bob LearnedCommented:
I don't use the EditItemTemplate normally, only the ItemTemplate, with a DropDownList, and I haven't had that exception, so I was kind of curious what you had done to get that error.  I would be curious to see what affect using ItemTemplate vs. EditItemTemplate would have.

Bob
0
 
gigasausageAuthor Commented:
well.. the only time you see this box would be when you click on edit. Otherwise you will see the selected value that you chose in a textbox. That way you dont have a messy "view" area if all you want to do is look at the data.
0
 
Bob LearnedCommented:
This is how I do it:

   Use a DropDownList all the time, instead of only on an edit item.  Set the SelectedItem for the DropDownList control to display the correct value.  I don't have any edit buttons, so the records are editable as is.  Since it works for me, I haven't tried it different ways.

Bob
0
 
gigasausageAuthor Commented:
it doesnt change the problem though wherever it is. If there is a null field in the gridview dataset that you are trying to bind to and not a corresponding null value in the dropdown dataset, I get the error because the dropdwown doesnt know what to do.
0
 
gigasausageAuthor Commented:
in classic ASP i would do this

<%
dim i
%>
<select name='dropdpwn'>
     <option value ="">
     <%
     for i = 0 to ubound(dropdowndataset,2)
     %>
            <option <%if SelectedValue = dropdowndataset(0,i) then%>selected<%end if%>>dropdowndataset(1,i)</option>
    <%next%>
</select>


this way i have a "" autoselected if none of the SelectedValues match a value in the dropdowndataset

How do i do this in asp.net 2.0 VS 2005?
0
 
gigasausageAuthor Commented:
this is how i finally got it to work...

Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) _
         Handles DetailsView1.DataBound
        Select Case DetailsView1.CurrentMode
            Case DetailsViewMode.Edit
                Dim myDataRowView As Data.DataRowView = DetailsView1.DataItem
                If myDataRowView("BuilderID") IsNot DBNull.Value Then
                    Dim myDropDownList As DropDownList = _
                    CType(DetailsView1.FindControl("DropDownList1"), DropDownList)
                    If myDropDownList.Items.FindByValue(myDataRowView("BuilderID")) _
                    IsNot Nothing Then
                        myDropDownList.Items.FindByValue(myDataRowView("BuilderID")).Selected = True
                    End If
                End If
        End Select
    End Sub
0
 
Bob LearnedCommented:
Cool 8-)  I was trying to come up with a similar example--ran out of time.

Bob
0
 
NetminderCommented:
Closed, 125 points refunded.
Netminder
Site Admin
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.