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

  • Question

  • User1028962535 posted

    <asp:DropDownList ID="ddlproducttype" class="form-control" runat="server"  DataSourceID="ProductType" DataTextField="ProductType" DataValueField="ProductTypeID"  BackColor="#FFFFCC" AutoPostBack="True" AppendDataBoundItems="True">
                     <asp:ListItem Value="0" Selected="True">Select</asp:ListItem>
    <asp:SqlDataSource ID="ProductType" runat="server" ConnectionString="<%$ ConnectionStrings:test %>" SelectCommand="SELECT ProductTypeID, ProductType FROM [ProductType]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="Products" runat="server" ConnectionString="<%$ ConnectionStrings:test %>" SelectCommand="SELECT [ProductID], [Product],[ProductTypeID] FROM [Products] WHERE ([ProductTypeID] = @ProductType)">
                        <asp:ControlParameter ControlID="ddlproducttype" Name="ProductType" PropertyName="SelectedValue" Type="Int32" />

    I have seen previous answers to questions like this one, but I am still having trouble fixing it.
    I have two dropdownlists on my a web form, that are linked as one filters the other - ProductType and Products.
    I have a button that acts as a search and takes the user to the last record entered, however I get the this error:

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

    Not sure why the values do not exist, do I need to check for nulls?..how can I fix this problem..thanks for any help.

    p.s other single dropdown lists work fine and return the value of the last record as expected, these are not filtering based on another

    See some of my code below

     conn = New SqlConnection(connectionstring)
    comm = New SqlCommand("latest", conn)
    comm.CommandType = System.Data.CommandType.StoredProcedure
    reader = comm.ExecuteReader()
    If reader.Read() Then
    ddlproducttype.Text = reader.Item("ProductType").ToString()  error here
    ddlproduct.Text = reader.Item("Product").ToString()
    End If
    Catch ex As Exception


    Friday, April 30, 2021 10:24 AM

All replies

  • User475983607 posted

    The code tries to set the selected value but this values does not exist in the DropDownlist.   Unfortunately,  there is no indication in the code where ddlproducttype is populated or the text values.  Also we cannot see the value of "ProductType".

    But you can see these values with the Visual Studio debugger.  I recommend setting a break point and debugging our code.

    Friday, April 30, 2021 11:26 AM
  • User-1716253493 posted
            If Not ddlproducttype.Items.FindByText(reader.Item("ProductType").ToString()) = Nothing Then
                ddlproducttype.Items.FindByText(reader.Item("ProductType").ToString()).Selected = True
            End If

    To find value

            If Not ddlproducttype.Items.FindByValue(reader.Item("ProductType").ToString()) = Nothing Then
                ddlproducttype.Items.FindByValue(reader.Item("ProductType").ToString()).Selected = True
            End If

    Friday, April 30, 2021 11:39 AM
  • User1535942433 posted

    Hi Dan5,

    According to your description,a good way is to find the value and check if it's nothing.

    And I think,you may check whether you have same problems. You may delete clear the trailing spaces of the data in the database table.

    Use this:

    <asp:DropDownList ID="DropDownList1" runat="server"  SelectedValue='<%# Bind("Item")%>'>

    Best regards,

    Yijing Sun

    Monday, May 3, 2021 3:30 AM