DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

Last post 12-27-2007 12:42 PM by downseven. 15 replies.

Sort Posts:

  • DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    07-21-2005, 11:48 AM

    Scenario:
    - DetailsView bound to an ObjectDataSource (both within a WizardStep if that's relevant)
    - DropDownList within a template field, bound to an XMLDataSource, with a SelectedValue bound to a column from the DetalsView. Also has AutoPostBack set to true.

    The DetailsView is in EditMode, which I want permenatly. I don't want the user explicitly enter edit mode, nor do I want them to hit a save button. So, when the list posts back I call DetalsView1.UpdateItem(), to force the update, which should refresh other values on the details view which are dependent upon the value in the list (it's delivery charges for a shopping cart - the sales tax plus cart total needs to be updated based upon the delivery charge).

    Problem:

    DropDownList1 has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value.

    However, in Page_PreRender I've checked both the SelectedValue and the list items in the DropdownList and the selected value is in the list? What's happening in the control binding that thinks it isn't?

    Dave

  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    07-21-2005, 1:47 PM
    I've solved this by removing the binding from the SelectedValue and manually adding and setting the parameter value in the Updating event of the ObjectDataSource. It's something I was hoping to avoid as for this particular site I'm trying to avoid code as much as possible. I'd still be interest why the original problem occured though.

    d
  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    09-27-2006, 12:30 PM

    I had this same issue and think that i have the solution:

     Add this code to each ddl:

    AppendDataBoundItems="True">
                        <asp:ListItem Value="">(None)</asp:ListItem>
     

  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    10-17-2006, 7:21 AM
    • Loading...
    • dan p
    • Joined on 10-10-2006, 8:57 AM
    • Posts 3

    Well done!

    In fact, setting  

    AppendDataBoundItems="True"
    seems to be sufficient.

    Thanks for the post.

  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    10-24-2006, 9:02 PM

    Hi,

    My codo is still didn't run:

    FCCode:
                     <asp:ObjectDataSource ID="ODSFacilities" runat="server"
                    SelectMethod="getDisFacByDistrictId" TypeName="templateTableAdapters.tblDistrictFacilitiesTableAdapter" OldValuesParameterFormatString="original_{0}">
                    <SelectParameters>
                        <asp:ProfileParameter Name="districtCode" PropertyName="districtId" Type="String" DefaultValue="<%$ Code:Profile.districtId %>" />
                    </SelectParameters>
                </asp:ObjectDataSource>
                    <asp:DropDownList ID="DropDownList1" runat="server"  AppendDataBoundItems="True"
                    DataSourceID="ODSFacilities" DataTextField="disFacName" DataValueField="disFacCode"
                    SelectedValue='<%# Bind("FCCode") %>' >
                                        <asp:ListItem Value="">(None)</asp:ListItem>
                    </asp:DropDownList>

    it still error:

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

    Help me please! 

  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    10-24-2006, 9:11 PM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,671
    • TrustedFriends-MVPs

    That seems simple enough; you need to make sure that your selected value is in the list :)

    Have you run this through the debugger to see if the list contains any items?

     

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    12-14-2006, 1:23 PM
    • Loading...
    • vanecek
    • Joined on 12-14-2006, 6:18 PM
    • Posts 3
    Main problem is in the comparation of selected value and value in the listbox. This problem is caused by length of string field. Use ltrim() function in select queries for grid and for dropdownlist too.
  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    12-14-2006, 1:28 PM
    • Loading...
    • vanecek
    • Joined on 12-14-2006, 6:18 PM
    • Posts 3
    Im sorry. I mean function rtrim() of course. Not ltrim.
  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    02-07-2007, 4:22 AM
    • Loading...
    • mfrimu
    • Joined on 01-01-2007, 2:12 AM
    • Posts 10

    I have the same problem, only all above solutions fail.

    The selectedvalue is bound to a varchar(15) field in the Database, which contains exactly the ListItem of the DropDownList. I use it in a DetailsView. Actually, in Insert mode the DropDownList is used to fill the table and it works, but when going into Edit mode the error pops up. Go figure.

    <asp:DropDownList ID="DropDownList2i" runat="server"  selectedvalue='<%# Bind("type") %>' >
     <asp:ListItem>Yes/No</asp:ListItem>
     <asp:ListItem>Multiple choice</asp:ListItem>
     <asp:ListItem>Numeric</asp:ListItem>
    </asp:DropDownList>

    Like I said adding AppendDataBoundItems="true" or an empty ListItem has no impact. The DataRow DOES contain the exact ListItem, just that the DropDown won't bind.

  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    02-07-2007, 5:33 AM
    • Loading...
    • mfrimu
    • Joined on 01-01-2007, 2:12 AM
    • Posts 10

    Found my own solution:

    It's the second time I have an apparent 'bug' but it's because i'm using unofficial reserved words for database field names. This time i used 'type' as a field name. That was causing all the problems. Renamed to QuestionType and now everything works fine. Last time i had used 'new' as a field name and had all kinds of weird behaviour.

    Lesson: I will always use 2 words to name database fields.....

     

  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    02-07-2007, 11:21 AM
    • Loading...
    • mfrimu
    • Joined on 01-01-2007, 2:12 AM
    • Posts 10

    While at it:

    I also noticed that fixed lenght fields such as char(10) don't bind to DropDownLists, i guess one could leave spaces at end of each choice and make sure thay are all thefull lenght of the field...

     Cheers

     

  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    03-14-2007, 10:51 AM

    In my case I had 2 dropdowns(Categories and SubCategories) where SubCategories items depend on choice of the Category selected. so AppendDataBoundItems = "True for me did not make sence.

    I found the this code to work for OnSelectedIndexChanged event of the first (Categories) dropdown

    cboSubCategories.DataSourceID = ""; //clear datasource

    cboSubCategories.DataSource = null;  //clear this way as well

    cboSubCategories.DataBind();    //!!! required to complete clearing

    //rebind

    /* set select parameted for the datasource for second drop down */

    ObjectDataSourceSubCategories.SelectParameters[0].DefaultValue = ((

    DropDownList)sender).SelectedValue;   ObjectDataSourceSubCategories.Select();

    cboSubCategories.DataSource = ObjectDataSourceSubCategories;

    cboSubCategories.DataBind();

    Also I wanted to have "(None)" as a first choice so I put that in OnDataBound event handler of the second dropdown

    DropDownList cboSubCategories = (DropDownList)sender;

    cboSubCategories.Items.Insert(0,

    new ListItem("(None)", "-1"));
  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    04-17-2007, 9:01 AM
    • Loading...
    • cdutoit
    • Joined on 11-30-2006, 12:38 PM
    • Posts 3

    Te easiest way I found with the object datasource is to set the dropdown list up like this

    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="ID" DataTextField="Text"
    DataValueField="ID" SelectedValue='<%# Eval("ID") != null ? Bind("ID") : "" %>' AppendDataBoundItems=true>

    <asp:ListItem Text="" Value=""></asp:ListItem>

    </asp:DropDownList>

    This will bind the item to "" if the value is null

  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    05-03-2007, 1:13 AM
    • Loading...
    • Shorin
    • Joined on 04-29-2007, 11:19 PM
    • Posts 1

    HOLY HECK This guy is RIGHT. I had this problem when I was filling my drop down with items manually. Its because there DOES need to be extra spaces at the end. Any database (except maybe MySQL) seems to always limit the length of text fields somehow. Oracle, Access, MSSQL all do it. This problem probably doesn't happen in MySQL if the text field lengths are truly variable and match the exact size of the field's data (i.e. your SelectedValue and the database value will match in length always).

     

    COol thanks for the solution! I was nearly losing it there... 

     

    mfrimu:

    While at it:

    I also noticed that fixed lenght fields such as char(10) don't bind to DropDownLists, i guess one could leave spaces at end of each choice and make sure thay are all thefull lenght of the field...

     Cheers

     

  • Re: DropDownList within a DataView and with a bound SelectedValue which "does not exist in the list"

    05-03-2007, 8:48 AM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,671
    • TrustedFriends-MVPs

    This is totally dependent on your database setup.  If you use varchar fields that are long enough to accommodate your data, then you don't need to worrry about trailing spaces.  I don't know of any serious professionals who use char for variable length fields on a regular basis.

     

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.