First, I searched the forums and couldn't find a thread that matched my problem. I also searched Google for my error message (in quotes) and received 9 results. If you know of a post/article that deals with my specific problem, I'd love to read it.

I have a TemplateField in a GridView that consists of a textbox that's populated from another database, specifically, I'm pulling "CategoryName" from my [Categories] table, and upon the update command, I want to insert its value into the "Category" column of my [Activities] table. When I click "edit" on one of the items, I get this error:

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

There's another dropdown box on my page (in a DetailsView) that works just fine with this code, and I also tested it in a dropdown box on other parts of my page just to be sure. They all populate correctly, and the one in the DetailsView even creates a new record with the value just as I want it to. I even ran the query on the back end, and it worked too.

Here's what I have in my GridView's EditItemTemplate:

<EditItemTemplate>
    <asp:DropDownList ID="ddl1" runat="server" 
        DataSourceID="sds2" DataTextField="CategoryName" 
        DataValueField="CategoryName" SelectedValue='<%# Bind("Category") %>' >
    </asp:DropDownList>
</EditItemTemplate>

And my DetailView's EditItemTemplate (identical to the GridView's code as far as I can tell):

<EditItemTemplate>
    <asp:DropDownList ID="ddl2" runat="server" 
        DataSourceID="sds2" DataTextField="CategoryName" 
        DataValueField="CategoryName" SelectedValue='<%# Bind("Category") %>'>
    </asp:DropDownList>
</EditItemTemplate>

And my SqlDataSource in case you need to see it:

<asp:SqlDataSource ID="sds2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
    SelectCommand="SELECT [CategoryName] FROM [Categories] ORDER BY [CategoryName]" >
</asp:SqlDataSource>

I've been at this for most of the weekend now, and have tried many things, including:

  1. Creating a new SqlDataSource just before/after my DDL that also pulls the [CategoryName].
  2. Creating one "dummy" item in the box, setting it to selected by default, and AppendDataboundItems to it.
  3. Tried every combination of DataTextField & DataValueField I can think of.

I'm about to the point of trying just manually entering all the possible values for the field, and not binding it at all, but I'd really like to use the data binding to my [Categories] table.

Do I need to some sort of INNER JOIN statement in my GridView's SelectCommand since I'm using data from another table (even though I pull the data from the other table via another data source)?

If you need me to post any more code, or give you any more information, I'd be happy to. ANY help would be greatly appreciated. Thanks so much in advance.

Recommended Answers

All 2 Replies

hi

asp:SqlDataSource ID="sds2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
    SelectCommand="SELECT [CategoryName] FROM [Categories] ORDER BY [CategoryName]" >
</asp:SqlDataSource><asp:SqlDataSource ID="sds2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
    SelectCommand="SELECT [CategoryName] FROM [Categories] ORDER BY [CategoryName]" >
</asp:SqlDataSource>

from the error i understand there is no item by the name specified in your category column.check that once.

hi

asp:SqlDataSource ID="sds2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
    SelectCommand="SELECT [CategoryName] FROM [Categories] ORDER BY [CategoryName]" >
</asp:SqlDataSource><asp:SqlDataSource ID="sds2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
    SelectCommand="SELECT [CategoryName] FROM [Categories] ORDER BY [CategoryName]" >
</asp:SqlDataSource>

from the error i understand there is no item by the name specified in your category column.check that once.

Thanks, but I figured it out shortly after I posted this. Here's what I finally did http://forums.asp.net/p/1397346/3009579.aspx#3009579.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.