Solved

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

Posted on 2006-07-14
19
3,838 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
19 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

627 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