Sql连接下拉列表

时间:2012-10-18 19:02:01

标签: c# asp.net sql

当我将下拉列表连接到两列时,我在删除sql数据时遇到问题。我想结合姓氏(lName)+名字(fName)并拉上这些名字的数据。这是我到目前为止所拥有的。

 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        DataSourceID="SqlDataSource3"  DataTextField="lName" DataValueField="lName" 
        AppendDataBoundItems="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem Value="Select">Select</asp:ListItem>
    </asp:DropDownList>

  <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TestDBConnectionString1 %>" 
        SelectCommand="SELECT (lName + fName) AS zName FROM CoaTest"></asp:SqlDataSource>

然后我有一个包含所有数据包的formview,它的sql连接如下所示:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TestDBConnectionString1 %>" 
        DeleteCommand="DELETE FROM [CoaTest] WHERE [Id] = @Id" 
        SelectCommand="SELECT Id, fName, lName, agency, title, gen, hired, AppS,
        course, Location, SessNum, SDate, EDate, Hours, SitUps, pushUps, rTime, 
        Grade FROM CoaTest WHERE (lName = @lName)"


        UpdateCommand="UPDATE [CoaTest] SET [fName] = @fName, [lName] = @lName, [agency] = @agency, [title] = @title, [gen] = @gen, [hired] = @hired, [AppS] = @Apps, [course] = @course, [Location] = @Location, [SDate] = @SDate, [EDate] = @EDate, [Hours] = @Hours WHERE [Id] = @Id">
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <SelectParameters> 
            <asp:ControlParameter ControlID="DropDownList1" Name="lName"  
                PropertyName="SelectedValue" Type="String" /> 

        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="fName" Type="String" />
            <asp:Parameter Name="lName" Type="String" />
            <asp:Parameter Name="Agency" Type="String" />
            <asp:Parameter Name="title" />
            <asp:Parameter Name="gen" />
            <asp:Parameter Name="hired" />
            <asp:Parameter Name="Apps" />
            <asp:Parameter Name="course" />
            <asp:Parameter Name="Location" />
            <asp:Parameter Name="SDate" />
            <asp:Parameter Name="EDate" />
            <asp:Parameter Name="Hours" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

如果我只是通过Last Name(lName)拉它,但是当我尝试连接它时,它不会从formview中提取数据。提前谢谢!!

2 个答案:

答案 0 :(得分:1)

将下拉选项命令更改为SELECT Id, (lName + fName) AS zName FROM CoaTest,将下拉列表的DataValueField值更改为Id,然后按ID而不是按名称选择数据到FormView

答案 1 :(得分:0)

我认为DataValueFieldDataTextField属性应如下:

  • datavaluefield ="zname",因为连接的别名名为zname no lname

此外,您不是在数据源查询中预测或显示lname表列