ASP.NET Web Form: Unable to Resolve Error in Template Field

I have an ASP.NET web form that is throwing an error I can't seem to work out.  I have confirmed that all of the data sources are working as expected however the item template and the edit template in the template field within the datagrid causes the error with the following message:

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

Stack Trace:
at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) at System.Web.UI.WebControls.ListControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at System.Web.UI.Control.DataBindChildren() at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.Control.DataBind() at System.Web.UI.Control.DataBindChildren() at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.Control.DataBind() at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) at System.Web.UI.WebControls.DataBoundControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at System.Web.UI.WebControls.GridView.DataBind() at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() at System.Web.UI.Control.EnsureChildControls() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Query String

Can anyone help me identify the problem?

Page:
<%@ Page Title="Edit Schedule" Language="C#" MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" CodeFile="ScheduleEdit.aspx.cs" Inherits="Management_Group_ScheduleEdit" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cpHC" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMC" Runat="Server">
    <h2>
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>: Edit Schedule</h2>
    <asp:GridView ID="gvEditSchedule" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataKeyNames="Id" DataSourceID="dsScheduleEdit">
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
            <asp:BoundField DataField="IntervalStartDate" HeaderText="Start Date" SortExpression="IntervalStartDate" ReadOnly="True" />
            <asp:BoundField DataField="TeamNumber" HeaderText="Team No" SortExpression="TeamNumber" ReadOnly="True" />
            <asp:TemplateField HeaderText="Participant" SortExpression="Participant">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dsMembers" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("Participant") %>'>
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dsMembers" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("Participant") %>'>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="dsScheduleEdit" runat="server" ConnectionString="<%$ ConnectionStrings:dbCS %>" SelectCommand="SELECT * FROM [Schedule] WHERE (([IntervalStartDate] &gt;= @IntervalStartDate) AND ([IntervalStartDate] &lt;= @IntervalStartDate2)) ORDER BY [IntervalStartDate], [TeamNumber], [Participant]">
        <SelectParameters>
            <asp:SessionParameter Name="IntervalStartDate" SessionField="SearchStartDate" Type="DateTime" />
            <asp:SessionParameter Name="IntervalStartDate2" SessionField="SearchEndDate" Type="DateTime" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="dsMembers" runat="server" ConnectionString="<%$ ConnectionStrings:dbCS %>" SelectCommand="spClientGroupMemberSortNames" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:SessionParameter Name="GroupID" SessionField="GroupID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
</asp:Content>

Open in new window


Sample Schedule Data:
Sample Schedule Data
LVL 1
penlandtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

When you click Edit,  the item Which you are trying to set as DropDownList SelectedValue = Participant, that  ID is not present in the DropDownList items.
0
penlandtAuthor Commented:
Thanks for the reply.  I know that's what the error means, but when I check the value of the session variable (GroupID) that serves as the parameter for the dsMembers DataSource it has the correct value, and when I manually execute the stored procedure that supplies the data for that DataSource using that parameter it returns the correct data.  It is not possible (in my database design) for a Participant value to exist in the Schedule table that doesn't also exist in the Members table.  The problem I'm currently having is that I don't know how to see what (if anything) the stored procedure is returning in real time.  Any ideas?  Thanks!

EDIT:  I forgot to mention that I was using vS Express 2013 for Web so if there's a tool in there (or elsewhere) that will allow me to see what data the stored procedure is returning in real time, I'm not aware of it.
0
Lokesh B RDeveloperCommented:
Hi,

1. Remove the SelectedValue in EditTemplate as below
2. Add a Label to see the SelectedValue.

Now, Check the Label Value is Present in the DropDownList Items.

 
<asp:GridView ID="gvEditSchedule" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataKeyNames="Id" DataSourceID="dsScheduleEdit">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                    <asp:BoundField DataField="IntervalStartDate" HeaderText="Start Date" SortExpression="IntervalStartDate" ReadOnly="True" />
                    <asp:BoundField DataField="TeamNumber" HeaderText="Team No" SortExpression="TeamNumber" ReadOnly="True" />
                    <asp:TemplateField HeaderText="Participant" SortExpression="Participant">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dsMembers" DataTextField="Name" DataValueField="Id">
                            </asp:DropDownList>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Participant") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dsMembers" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("Participant") %>'>
                            </asp:DropDownList>

                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.