Link to home
Start Free TrialLog in
Avatar of Todd Penland
Todd PenlandFlag for United States of America

asked on

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:
User generated image
Avatar of Lokesh B R
Lokesh B R
Flag of India image

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.
Avatar of Todd Penland

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial