Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-07-14
19
Medium Priority
?
3,842 Views
Last Modified: 2008-10-05
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
0
Comment
Question by:gigasausage
  • 10
  • 7
18 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17109996
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
 

Author Comment

by:gigasausage
ID: 17110012
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17110036
So, the ID field isn't a primary key?  Are you are talking about the Name property?  

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:gigasausage
ID: 17110089
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
 

Author Comment

by:gigasausage
ID: 17110111
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17110165
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
 

Author Comment

by:gigasausage
ID: 17110172
<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
 

Author Comment

by:gigasausage
ID: 17110220
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17110521
>>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
 

Author Comment

by:gigasausage
ID: 17110555
<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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17110608
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
 

Author Comment

by:gigasausage
ID: 17110634
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17110747
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
 

Author Comment

by:gigasausage
ID: 17111274
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
 

Author Comment

by:gigasausage
ID: 17111329
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
 

Author Comment

by:gigasausage
ID: 17111847
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17112291
Cool 8-)  I was trying to come up with a similar example--ran out of time.

Bob
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 17157083
Closed, 125 points refunded.
Netminder
Site Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question