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,832 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
19 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
So, the ID field isn't a primary key?  Are you are talking about the Name property?  

Bob
0
 

Author Comment

by:gigasausage
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
<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
Comment Utility
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
Comment Utility
>>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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:gigasausage
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Closed, 125 points refunded.
Netminder
Site Admin
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now