Hi all,
I'm getting the dreaded 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value error when trying to filter a drop down list in a templatefield using one of the other boundfield values (I'm trying to get a list of employees based on their department - i.e. the user can change the employee but only to another member of the same department).
Here's the code:
<asp:GridView ID="Rotas" runat="server" AllowSorting="True"
DataSourceID="SqlDataSource3" AutoGenerateEditButton="True" DataKeyNames="DateFrom,DateTo,DepartmentId"
AutoGenerateColumns="False" OnRowUpdating="Rotas_RowUpdating">
<Columns>
<asp:BoundField DataField="DateFrom" HeaderText="DateFrom" ReadOnly="True"
SortExpression="DateFrom" />
<asp:BoundField DataField="DateTo" HeaderText="DateTo" ReadOnly="True"
SortExpression="DateTo" />
<asp:TemplateField HeaderText="Employee Name" SortExpression="EmployeeName">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource4" DataTextField="EmployeeName"
DataValueField="EmployeeName" SelectedValue='<%# Bind("EmployeeName") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DepartmentId" HeaderText="DepartmentId"
ReadOnly="True" SortExpression="DepartmentId" />
<asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" ReadOnly="False"
SortExpression="EmployeeId" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>"
SelectCommand="SELECT r.DateFrom, r.DateTo, e.EmployeeName, e.EmployeeId, r.departmentid
FROM
dbo.[Rota] r INNER JOIN
dbo.[Employee] AS e ON r.EmployeeId = e.EmployeeId
WHERE (r.DateTo >= GETDATE()) "
UpdateCommand="UPDATE [Rota] SET [EmployeeId] = (select employeeid from employee where employeename = @EmployeeName),
[departmentid] = (select departmentid from employee where employeename = @EmployeeName)
WHERE [DateFrom] = @DateFrom AND [DateTo] = @DateTo AND [DepartmentId] = @DepartmentId">
<UpdateParameters>
<asp:Parameter Name="DateTo" Type="DateTime" />
<asp:Parameter Name="DateFrom" Type="DateTime" />
<asp:Parameter Name="DepartmentId" Type="Int16" />
<asp:Parameter Name="EmployeeId" Type="Int16" />
<asp:Parameter Name="EmployeeName" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>"
onselecting="SqlDataSource4_Selecting"
SelectCommand="SELECT [EmployeeName] FROM [Employee] where DepartmentId=@DepartmentId"
>
<SelectParameters>
<asp:ControlParameter ControlID="Rotas" Name="DepartmentId"
PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
Really can't see what I'm doing wrong. If I don't use the Select Parameter and just a 'select employeename from employee' then the whole list of employees is displayed fine. As soon as I try and use a controlparameter it falls over. Help! :)
Thanks in advance for any assistance offered.