【问题标题】:Multi-parameter SqlDatasource search not returning any records多参数SqlDatasource搜索不返回任何记录
【发布时间】:2020-02-28 13:08:20
【问题描述】:

我有一个奇怪的问题。我有一个附加到我发送三个可能参数的 sqldatasource 的 gridview。如果我搜索项目编号,则搜索工作正常,但如果我搜索没有项目编号的名字或姓氏,它不会返回任何内容。当我在 SQL 管理工作室中测试它时 SQL 工作正常,但当我在搜索表单中的 Visual Studio 中测试它时却没有。 解决此问题的任何帮助将不胜感激。感谢您的宝贵时间。

<table style="width: 100%;">

        <tr>
            <td>Caller Search</td>
            <td>
                <asp:Label ID="ITEMsearchlable" runat="server" Text="ITEM#"></asp:Label><br />
                <asp:TextBox ID="ITEM_NUMsearch" runat="server"></asp:TextBox></td>
            <td>
                <asp:Label ID="ofname_label" runat="server" Text="First name"></asp:Label><br />
                <asp:TextBox ID="search_fname" runat="server"  CssClass="uppercase"></asp:TextBox></td>
            <td><asp:Label ID="olname_label" runat="server" Text="Last name"></asp:Label><br />
                <asp:TextBox ID="search_lname" runat="server"   CssClass="uppercase"></asp:TextBox></td>
            <td>
                <asp:Button ID="search_btn" runat="server" Text="Offender Caller" /></td>
        </tr>

    </table>

          <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" Width="771px" EmptyDataText="No record found">
              <Columns>
                  <asp:CommandField ShowSelectButton="True" />
                  <asp:BoundField DataField="ITEM_NUM" HeaderText="ITEM_NUM" SortExpression="ITEM_NUM" />
                  <asp:BoundField DataField="NAME_FIRST" HeaderText="NAME_FIRST" SortExpression="NAME_FIRST" />
                  <asp:BoundField DataField="NAME_LAST" HeaderText="NAME_LAST" SortExpression="NAME_LAST" />
                  <asp:BoundField DataField="CALLER_ID" HeaderText="CALLER_ID" SortExpression="CALLER_ID" />
              </Columns>
          </asp:GridView>
          <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
              ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>" 
              SelectCommand="SELECT DISTINCT
                            ORDERS.ITEM_NUM, 
                            ORDERS.NAME_FIRST, 
                            ORDERS.NAME_LAST, 
                            VICTIM_CALL_LOG.CALLER_ID
                            FROM ORDERS LEFT OUTER JOIN
                            VICTIM_CALL_LOG ON ORDERS.ITEM_NUM = VICTIM_CALL_LOG.ITEM_NUM WHERE ((ORDERS.ITEM_NUM = @ITEM_NUM) 
                            OR (ORDERS.NAME_FIRST LIKE '%' + @NAME_FIRST + '%') OR (ORDERS.NAME_LAST LIKE '%' + @NAME_LAST + '%'))">
              <SelectParameters>
                  <asp:ControlParameter ControlID="ITEM_NUMsearch" Name="ITEM_NUM" PropertyName="Text" Type="Int32" />
                  <asp:ControlParameter ControlID="search_fname" DefaultValue="NULL" Name="NAME_FIRST" PropertyName="Text" Type="String" />
                  <asp:ControlParameter ControlID="search_lname" DefaultValue="NULL" Name="NAME_LAST" PropertyName="Text" Type="String" />
              </SelectParameters>
          </asp:SqlDataSource>

【问题讨论】:

  • 只是为了检查一下,您能否从查询中取出%s,现在将它们输入search_fname? ISTR 在尝试在查询中而不是在参数值中添加通配符时出现一些问题。
  • 我试过了,不幸的是问题仍然存在。
  • 奇怪的是,如果我从搜索中删除 ITEM_NUM 并且 sql 它可以正常工作,但只有在我搜索 ITEM 编号时它才有效。好奇怪。
  • 如果你给 ControlParameter ITEM_NUM 一个默认值"-1",会有帮助吗?
  • 那行得通。谢谢。我不知道为什么我没有想到默认值。非常感谢!

标签: sql asp.net vb.net


【解决方案1】:

你也需要给数值参数一个默认值:

<asp:ControlParameter ControlID="ITEM_NUMsearch" DefaultValue="-1" Name="ITEM_NUM" PropertyName="Text" Type="Int32" />

我使用“-1”是因为我猜测它永远不会是数据库中的实际值,因此需要检查。

请注意,字符串参数中的 DefaultValue 为“NULL”实际上是字符串“NULL”,而不是 NULL 的数据库版本。为了正确地做到这一点,我怀疑您必须使用代码隐藏而不是 &lt;asp&gt; 控件。同时,默认值“12345”可能是更好的选择,以防万一有人的名字中包含“null” - Null 是某些人的实际姓氏:How does a surname of Null cause problems in many databases?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    相关资源
    最近更新 更多