我知道這個題目是所有的地方,但我沒有做一個INSERT
,UPDATE
,DELETE
。我的陳述是一個簡單而簡單的SELECT
聲明,迄今爲止我已經在我的數據庫中處理了116個不同的項目,直到我找到一個。子查詢返回多個值
我有一個搜索引擎,並打算通過每一個產品在我們的數據庫將信息添加到它。這一切都是通過網站完成的,但是當我搜索ProductID 331並點擊它時,它會轉到錯誤頁面,說Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
這對我來說沒有任何意義,只有這個網站會出錯一種產品。 這是我正在使用的聲明。有誰知道爲什麼1個產品會導致這個錯誤?
的WebService:
Public Class ProductSearch
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetProducts(ByVal prefixText As String, ByVal count As Integer)
As String()
Dim ProductSql As String = "Select DISTINCT ProductID, ProductName
FROM Product WHERE ProductName
LIKE '%' & @prefixText & '%'
ORDER BY ProductName ASC"
Using sqlConn As New SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings
("LocalSqlServer").ConnectionString)
sqlConn.Open()
Dim myCommand As New SqlCommand(ProductSql, sqlConn)
myCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50)
.Value = prefixText
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
Dim myTable As New DataTable
myTable.TableName = "ProductSearch"
myTable.Load(myReader)
sqlConn.Close()
Dim items As String() = New String(myTable.Rows.Count - 1) {}
Dim i As Integer = 0
For Each dr As DataRow In myTable.Rows
Dim id As String = dr("ProductID").ToString()
Dim name As String = dr("ProductName").ToString()
Dim item As String = AjaxControlToolkit.AutoCompleteExtender
.CreateAutoCompleteItem(name, id)
items.SetValue(item, i)
i += 1
Next
Return items
End Using
End Function
End Class
aspx頁面調用web服務:
<%@ Page Title="Product Search" Language="VB" MasterPageFile="~/MasterPage.master"
AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
function AutoCompleteClientMethod(source, eventArgs) {
var value = eventArgs.get_value();
window.location = ("/Product/Default.aspx?id=" + value)
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="ProductSearch.asmx" />
</Services>
</asp:ScriptManager>
<asp:TextBox ID="Search" runat="server" AutoComplete="off"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="Search" ServicePath="~/ProductSearch.asmx"
ServiceMethod="GetProducts" MinimumPrefixLength="1" CompletionSetCount="120"
EnableCaching="true" OnClientItemSelected="AutoCompleteClientMethod">
</asp:AutoCompleteExtender>
</div><!--End of main div -->
</asp:Content>
更新:11/9/2011 - 我已經發現一對夫婦更多的記錄有這個問題。他們是產品ID 331-335。我不知道這裏發生了什麼。難道這些產品不是真的存在,或者它們有某種錯誤?
這裏是ProductIDs,並且有這樣的錯誤及其對應的ProductNames的列表:
122 'Managed account section of the Web Site'
331 'Elliott Wave Principle Key to Market Behavior'
332 'Targeting Profitable Entry & Exit Points'
333 'Essentials of Trading It's not WHAT You Think, It's HOW You Think'
334 'Exceptional Trading The Mind Game'
335 'Fibonacci Analysis'
中有沒有子查詢,你一定是你的代碼中完整的查詢?次要的問題:PrefixText將是更好的也可以是綁定變量,而不是字符串連接,以避免潛在的SQL注入 – Andrew
@Andrew我更新了我的代碼 – jlg
我沒有這使我想知道爲什麼這個錯誤是發生任何的子查詢。我能想到的唯一的事情是,當javascript函數嘗試重定向用戶時,它的功能會拉回多個值。 – jlg