• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 88
  • Last Modified:

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
0
penlandt
Asked:
penlandt
  • 2
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now