【问题标题】:Select query run in `Visual Studio` but not in browser选择在“Visual Studio”中运行但不在浏览器中的查询
【发布时间】:2013-11-20 10:37:55
【问题描述】:

当我使用查询生成器测试数据源查询时,我有一个连接到 SQLDataSource 的 datalist 控件,它返回数据集,但是当我在任何浏览器中运行网站时,查询不返回数据集。

It seems like the website is not recompiling properly however i am not getting any build errors.

有人对可能的解决方案有任何想法吗?

提前致谢

我的sql数据源代码是-

<asp:SqlDataSource ID="SqlDataSourceSearch" runat="server" ConnectionString="<%$ ConnectionStrings:BazaarCeramicsConnectionString %>" 
        SelectCommand="SELECT Categories.Name, Products.ProductID, Products.Name AS Expr1 FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID WHERE (Products.Name LIKE '%' + @SearchQuery + '%')">
        <SelectParameters>
        <asp:QueryStringParameter Name="SearchQuery" QueryStringField="search" 
            Type="String" />
    </SelectParameters>
    </asp:SqlDataSource>

我的数据列表代码是-

<asp:DataList ID="DataList1" runat="server" DataKeyField="ProductID" DataSourceID="SqlDataSourceSearch">
        <ItemTemplate>
            Name:
            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
            <br />
            ProductID:
            <asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
            <br />
            Expr1:
            <asp:Label ID="Expr1Label" runat="server" Text='<%# Eval("Expr1") %>' />
            <br />


这是我的意见-

<div class="searchBox">
                    <asp:TextBox ID="SearchTextBox" runat="server"></asp:TextBox>
                    <asp:Button ID="SearchButton" runat="server" onclick="Button1_Click" Text="Search" />
                    <a href="ShoppingCart.aspx"><img id="cartIcon" src="Images/shoppingCartIcon.png" /></a>
               </div>

下面是代码

 protected void Button1_Click(object sender, EventArgs e)
{
    string SearchTerm = SearchTextBox.Text;
    Response.Redirect("SearchResults.aspx?SearchQuery=" + Server.UrlEncode(SearchTerm));
}

这是我的连接字符串-

<add name="BazaarCeramicsConnectionString" connectionString="Data Source=CATHERINE\SQLEXPRESS;Initial Catalog=BazaarCeramics;User ID=****;Password=******"
  providerName="System.Data.SqlClient" />

【问题讨论】:

  • 请添加SQLDatasource的相关代码以及您要显示的位置。
  • 您能否显示代码以及您为SearchQuery 参数设置的值
  • 您正在使用 QueryStringParameter,在您的 URL 中,您看到的是 search=1 还是其他变量?它应该类似于 www.test.com/Testing/page.aspx?search=1... 如果没有搜索参数或为空,则不会返回结果。
  • 嗨,Bhavesh,我已经添加了我正在使用的搜索框、按钮和代码
  • 嗨,Humpy 是的,查询附加到 URL,如图所示,我知道这是一个在我的数据库中多次出现的项目,并且相同的 seach 在查询生成器中返回结果。 localhost/SearchResults.aspx?SearchQuery=Bambi

标签: asp.net sql sql-server select visual-studio-2013


【解决方案1】:
(Products.Name LIKE '%' + @SearchQuery + '%')

【讨论】:

    猜你喜欢
    • 2019-03-16
    • 2022-01-10
    • 2018-09-12
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多