开发者

Error trying to filter drop down list with a Gridview field value via a controlparameter

开发者 https://www.devze.com 2023-01-04 16:17 出处:网络
I\'m getting the dreaded \'DropDownList1\' has a SelectedValue which is invalid because it does not exist in the list of items.

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" HeaderT开发者_如何学运维ext="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.


I think the problem is that your SqlDataSource that returns the list of employees for a department isn't returning any rows, and I think this is because the ControlParameter isn't right. Although you've done the right thing by pointing it at the Rotas GridView and the SelectedValue property, there are three fields used in the DataKeyNames property (DateFrom, DateTo, DepartmentId), and I believe you're currently passing the DateFrom value into your query - hence, no results. What I think you need to use in the PropertyName of the ControlParameter instead of the SelectedValue property of Rotas is the SelectedDataKey - there's details on MSDN here although the demo code there isn't particularly useful. However the important line is:

If you are creating a ControlParameter object and want to access a key field other than the first field, use the indexed SelectedDataKey property in the PropertyName property of the ControlParameter object

So from that I think what you need is:

<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="SelectedDataKey[2]" Type="Int16" />               
</SelectParameters>
</asp:SqlDataSource>

Without a copy of your data I can't test it, but give it a go and see what you get...

0

精彩评论

暂无评论...
验证码 换一张
取 消