SelectedValue which is invalid because it does not exist in the list of items.

Discussion in '.NET' started by far, May 16, 2006.

  1. far

    far

    Joined:
    May 16, 2006
    Messages:
    1
    Hello,

    Especially Phillip. I am desparately trying to implement cascading dropdowns with in a gridview. However I can not even get past the basics.

    Here is the error I get. I am using the pubs database.
    'ddlCountry' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Please advise. Thanks, far

    Here is my code:

    <asp:ObjectDataSource ID="odsPublishers" runat="server" OldValuesParameterFormatString="original_{0}"
    SelectMethod="GetPublishers" TypeName="PubsTableAdapters.PublishersTableAdapter"></asp:ObjectDataSource>

    </div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataSourceID="odsPublishers" AllowPaging="True" AllowSorting="True" DataKeyNames="pub_id">
    <Columns>
    <asp:TemplateField HeaderText="Commands">
    <ItemTemplate>
    <asp:LinkButton CommandName="Edit" ID="btnEdit" runat="server" Text="Edit"></asp:LinkButton>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:LinkButton CommandName="Update" ID="btnUpdate" runat="server" Text="Update"></asp:LinkButton>
    <asp:LinkButton CommandName="Cancel" ID="btnCancel" runat="server" Text="Cancel"></asp:LinkButton>
    </EditItemTemplate>
    </asp:TemplateField>


    <asp:BoundField DataField="pub_id" HeaderText="pub_id" ReadOnly="True" SortExpression="pub_id" />
    <asp:BoundField DataField="pub_name" HeaderText="pub_name" SortExpression="pub_name" />

    <asp:BoundField DataField="country" HeaderText="country" SortExpression="country" />
    <asp:TemplateField HeaderText="Country">
    <ItemTemplate>
    <asp:Label ID="lblCountry" runat="server" Text='<%#Eval("Country") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:ObjectDataSource ID="odsCountry" runat="server" OldValuesParameterFormatString="original_{0}"
    SelectMethod="GetCountry" TypeName="PubsTableAdapters.pubCountryTableAdapter"></asp:ObjectDataSource>
    <asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="odsCountry" AutoPostBack="True"
    SelectedValue='<%# Bind("Country") %>'>
    </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateField>

    <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
    </Columns>
    </asp:GridView>
    far, May 16, 2006
    #1
    1. Advertising

  2. far

    garylamj

    Joined:
    Aug 1, 2006
    Messages:
    1
    I have gone throught that problem as well.

    just simply do something like this:

    <asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="odsCountries"
    AutoPostBack="True"
    SelectedValue='<%# Bind("Country") %>'
    AppendDataBoundItems="true">
    <asp:ListItem Value="">Select a Country</asp:ListItem>
    <asp:ListItem Value="">Your invalid data</asp:ListItem>
    <asp:ListItem Value="">Your invalid data</asp:ListItem>
    </asp:DropDownList>

    OR

    Fix up your database don't allow nulls.

    However, I need your help.

    I do the same things but whatever my dropdown value is chosen, it won't update my database. Can you suggest me a solution why?

    Thanks,
    Gary
    garylamj, Aug 1, 2006
    #2
    1. Advertising

  3. far

    mwrisner

    Joined:
    Dec 27, 2007
    Messages:
    1
    Try This!

    When I had this error message, it drove me nuts for solid day! Here's what I learned:

    If you're receiving this error, then an Eval("something") or Bind("something") is referencing a database column that is empty. Manually enter some data (using SQL Server Management Studio, perhaps), and see if the error doesn't go away.\

    Hope this helps. Good luck!!
    mwrisner, Dec 27, 2007
    #3
  4. far

    shaileshakgec

    Joined:
    Dec 29, 2007
    Messages:
    1
    Call only once

    I got same problem too. I had used a function to Bind drop down list. But i got that i was calling that function two times and i think due to calling two times, i was facing that error. Then i call that function only once and error resolved. If any one got better option then pls reply.
    shaileshakgec, Dec 29, 2007
    #4
  5. far

    bartekm

    Joined:
    Aug 17, 2008
    Messages:
    7
    bartekm, Aug 17, 2008
    #5
  6. far

    antonacheradu

    Joined:
    Jan 14, 2012
    Messages:
    1
    Try to add an UNION at your SQL SELECT phrase which populate your DropDownList:
    SELECT GroupeID, Groupe FROM [Groupe]
    union select '', ''

    This will give you an empty row for avoiding the error.
    antonacheradu, Jan 14, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page