【问题标题】:Populating dropdown list with SQL in Visual Studio but using a textbox in query在 Visual Studio 中使用 SQL 填充下拉列表,但在查询中使用文本框
【发布时间】:2018-04-13 06:15:20
【问题描述】:

我一直在尝试在 Visual Studio .net 中填充下拉列表,但每当我尝试在查询中使用文本框时,我一直在使用 Visual Studio 中的配置选择语句向导,尝试时出现错误在 sql select 命令中使用文本框,或者在我测试查询时给出空字段。

         <table class="auto-style1">
 <tr>
     <td class="auto-style1"></td>
       <td class="auto-style2">
           Select Class:</td>
            <td class="auto-style1">
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="ClassName" DataValueField="ClassName"></asp:DropDownList>



                     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [ClassName] FROM [Classroom] WHERE ([TeacherUsername] = @TeacherUsername)">
                         <SelectParameters>
                          <asp:FormParameter FormField="TextBox1.Text" Name="TeacherUsername" Type="String" />
                           </SelectParameters>
                        </asp:SqlDataSource>
                    </td>
                    <td class="auto-style1">
                        <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" Visible="False"></asp:TextBox>
                        <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
                    </td>
                </tr>
            </table>

那么谁能帮我在我的 sql 选择查询中包含我的文本框,谢谢!

【问题讨论】:

    标签: sql .net visual-studio


    【解决方案1】:

    只需修改您的 SelectCommand 删除 TeacherUsername 的 where 子句并为其添加过滤器:

    SelectCommand="SELECT [ClassName] FROM [Classroom]" FilterExpression="TeacherUsername= '{0}'"

    【讨论】:

    • 我试过这个方法,但是下拉菜单中仍然没有出现值,我也试过删除代码的参数行但仍然没有帮助。
    猜你喜欢
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多