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

  • Question

  • User-895853764 posted

     Hi all,

     I can't figure out why I get this message :

     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:bdConnectionString3 %>"
            SelectCommand="select id_pays,pays from [Pays] order by [pays]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:bdConnectionString3 %>"
            SelectCommand="select * from [clients]"></asp:SqlDataSource>

     <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource3">
            <EditItemTemplate>
     <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1"
                                    DataTextField="pays" DataValueField="id_pays" selectedvalue='<%# Bind("id_pays") %>'>
    </asp:DropDownList>
      </EditItemTemplate> 

    <%# Bind("id_pays") %> exists in dropdownlist3 ... I tryed to define the selectedvalue in the code behind on dropdownlist3 databound and it works if I declare it in a static way like : dropdownlist.selectedvalue = "FR" but doesn't if I try to use a variable.

    Any ideas ?

    Thank you in advance. 

    Monday, March 10, 2008 1:19 PM

Answers

  • User77042963 posted

    I don't know what data type is your column char or varchar?  You may need to use a small set of your data to test out this code first. Without seeing your data, it is difficult to continue for this issue. If the column is char type, you may need to use Trim function (Rtrim/Ltrim in sql) to remove white space.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 10, 2008 3:25 PM

All replies

  • User77042963 posted

    What about this?

    <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1"
                                    DataTextField="pays" DataValueField="id_pays" selectedvalue='<%# Bind("id_pays") %>' AppendDataBoundItems="True" >

    <asp:ListItem Value="">Select One</asp:ListItem>

    </asp:DropDownList>

    If this does not work, you need to check your data from your tables to see whether they are matching.

    Monday, March 10, 2008 1:41 PM
  • User-895853764 posted

     Thank you limno but I've already tryed that (and just did it again to check) and it still doesn't work.

    About checking my data :

    1) The 'DatavalueField' has a 'FR' value for sure (I checked without  the selectedvalue='<%# Bind("id_pays") %>' in the source code of the generated page).

    I tryed to do as well a  DropDownList3.selectedvalue = "FR" in the code behind and it worked.

    2) About the  <%# Bind("id_pays") %> itself, I'm sure that it returns 'FR' as well because I'd checked it with a response.write in the code behind ....

     Any other ideas ?

    Thank you
     

     
     

    Monday, March 10, 2008 2:07 PM
  • User77042963 posted

    If the data of the id_pays column from your FormView's datasource table include any value that does not exist in your DropDownList's value list, you will have this problem(even NULL values).

    Monday, March 10, 2008 2:28 PM
  • User-895853764 posted

     Ok, so I guess it's why you did your first suggestion ?

    I had some Null values inside my db so to make it simple I just put 'FR' on all the fields even the NULL ones and ... still the same error.

    Start to make me crazy [:(] 

    Monday, March 10, 2008 3:10 PM
  • User-184382143 posted

    hi On data binding of the the drop down list IIn case if he drop down is inside the datagrid , the batabind method of datagrid) use the below code to set the default value.

    strValue="Some value" if (lstAccept.Items.IndexOf(lstAccept.Items.FindByValue(strValue)) == -1) { lst = new ListItem(); lst.Value = strValue; lst.Text = strValue; lstAccept.Items.Add(lst); } lstAccept.SelectedIndex = lstAccept.Items.IndexOf(lstAccept.Items.FindByValue(strValue));

    Based on the requirement you just changed the values . Think this will help for you

    Monday, March 10, 2008 3:22 PM
  • User77042963 posted

    I don't know what data type is your column char or varchar?  You may need to use a small set of your data to test out this code first. Without seeing your data, it is difficult to continue for this issue. If the column is char type, you may need to use Trim function (Rtrim/Ltrim in sql) to remove white space.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 10, 2008 3:25 PM
  • User-895853764 posted

     Limno,

     Thank you so much ! When I saw your last message I felt really .... hum ... stupid ! Of course it was as simple as that, I had a char(2) and a char(10) fields in my tables ... thanks again I don't think I would have ever checked that ;p

     
    Thanks coolbond too for your contribution.
     

    Tuesday, March 11, 2008 6:40 AM