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
    Likes Received:
    0
    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. Advertisements

  2. far

    garylamj

    Joined:
    Aug 1, 2006
    Messages:
    1
    Likes Received:
    0
    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. Advertisements

  3. far

    mwrisner

    Joined:
    Dec 27, 2007
    Messages:
    1
    Likes Received:
    0
    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
    Likes Received:
    0
    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
    Likes Received:
    0
    bartekm, Aug 17, 2008
    #5
  6. far

    antonacheradu

    Joined:
    Jan 14, 2012
    Messages:
    1
    Likes Received:
    0
    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. Advertisements

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.
Similar Threads
  1. Dave
    Replies:
    1
    Views:
    10,304
    Steven Cheng[MSFT]
    Oct 24, 2005
  2. sree reddy
    Replies:
    2
    Views:
    2,098
    David C
    Jun 22, 2007
  3. Dave
    Replies:
    2
    Views:
    1,195
  4. Jason
    Replies:
    0
    Views:
    3,356
    Jason
    Aug 7, 2007
  5. J055
    Replies:
    4
    Views:
    8,444
    Steven Cheng[MSFT]
    Nov 14, 2007
  6. ken_wilsonii@hotmail.com
    Replies:
    1
    Views:
    857
    Just Me
    Mar 24, 2008
  7. randy.buchholz
    Replies:
    4
    Views:
    1,043
    randy.buchholz
    Jul 16, 2008
  8. APA
    Replies:
    0
    Views:
    302
Loading...