ASP搜索和结果在一个页面中

时间:2009-09-22 10:25:59

标签: asp-classic

我有一个经典的ASP页面,我想显示一个搜索表单和相关的结果。

当用户第一次访问此页面时,我想显示一个搜索表单和10个最新属性。如果用户决定使用搜索表单来检索更多相关属性,那么我希望将默认的10个最新属性替换为用户的分页搜索结果。

到目前为止,我的代码看起来像这样;

<head>
    <title>Search</title>
</head>
<body>

<div class="search">
<h3>Search form</h3>
<form id="form1" name="form1" method="post" action="gist188770.asp">
<label>Street: <input type="text" name="searchStreet" value="<%=Server.HtmlEncode(Request("searchStreet") & "") %>" /></label>
<label>Town: <input type="text" name="searchTown" value="<%=Server.HtmlEncode(Request("searchTown") & "") %>" /></label>
<input type="submit" name="Submit" value="Submit" />
</form>
</div>

<%

if (Request.ServerVariables("REQUEST_METHOD") = "POST") then

'arrived via post get form values and do search
Dim myRecordSet
Dim myRecordSet_numRows

Set myRecordSet = Server.CreateObject("ADODB.Recordset")
myRecordSet.ActiveConnection = MM_dbconn_STRING

'collect the form input
set objDBParam = objDBCommand.CreateParameter("@ContentStreet",200,1,100)
    objDBCommand.Parameters.Append objDBParam
    objDBCommand.Parameters("@ContentStreet") = Request.QueryString("searchStreet")
set objDBParam = Nothing
set objDBParam = objDBCommand.CreateParameter("@ContentStreet",200,1,100)
    objDBCommand.Parameters.Append objDBParam
    objDBCommand.Parameters("@ContentTown") = Request.QueryString("searchTown")
set objDBParam = Nothing
set objDBParam = objDBCommand.CreateParameter("@ContentStreet",200,1,20)
    objDBCommand.Parameters.Append objDBParam
    objDBCommand.Parameters("@ContentPostcode") = Request.QueryString("searchPostcode")
set objDBParam = Nothing

'check for a match
myRecordSet.Source = "SELECT *"
myRecordSet.Source = myRecordSet.Source& "FROM ("
myRecordSet.Source = myRecordSet.Source& "SELECT id"
myRecordSet.Source = myRecordSet.Source& "FROM ("
myRecordSet.Source = myRecordSet.Source& "SELECT id"
myRecordSet.Source = myRecordSet.Source& "FROM VWTenantPropertiesResults"
myRecordSet.Source = myRecordSet.Source& "WHERE ContentStreet LIKE '%" & "@ContentStreet" & "%'"
myRecordSet.Source = myRecordSet.Source& "UNION ALL"
myRecordSet.Source = myRecordSet.Source& "SELECT id"
myRecordSet.Source = myRecordSet.Source& "FROM VWTenantPropertiesResults"
myRecordSet.Source = myRecordSet.Source& "WHERE ContentTown LIKE '%" & "@ContentTown" & "%'"
myRecordSet.Source = myRecordSet.Source& "UNION ALL"
myRecordSet.Source = myRecordSet.Source& "SELECT id"
myRecordSet.Source = myRecordSet.Source& "FROM VWTenantPropertiesResults"
myRecordSet.Source = myRecordSet.Source& "WHERE ContentPostCode LIKE '%" & "@ContentPostcode" & "%'"
myRecordSet.Source = myRecordSet.Source& ") qi"
myRecordSet.Source = myRecordSet.Source& "GROUP BY"
myRecordSet.Source = myRecordSet.Source& "id"
myRecordSet.Source = myRecordSet.Source& "HAVING COUNT(*) >= 2"
myRecordSet.Source = myRecordSet.Source& ") q"
myRecordSet.Source = myRecordSet.Source& "JOIN VWTenantPropertiesResults r"
myRecordSet.Source = myRecordSet.Source& "ON r.id = q.id"
myRecordSet.Source = myRecordSet.Source& "WHERE ContentBedrooms BETWEEN 1 AND 4"
myRecordSet.Source = myRecordSet.Source& "AND ContentPrice BETWEEN 50 AND 500"
myRecordSet.Source = myRecordSet.Source& "ORDER BY"
myRecordSet.Source = myRecordSet.Source& "ContentPrice"

'display the results
if myRecordSet.BOF then
response.write("Latest properties:<br>")
do until myRecordSet.EOF
%>
<div class='result'>")
  <dl><%=myRecordSet("ContentTitle")%></dl>
  <dt><%=myRecordSet("ContentStreet")%></dt>
  <dt><%=myRecordSet("ContentTown")%></dt>
  <dt><%=myRecordSet("ContentPostcode")%></dt>
</div><%
myRecordSet.MoveNext
loop
end if

else
    'arrived via get show last 10 results
    Dim myRecordSet2
    Dim myRecordSet2_numRows

    Set myRecordSet2 = Server.CreateObject("ADODB.Recordset")
    myRecordSet2.ActiveConnection = MM_dbconn_STRING
    myRecordSet2.Source = "SELECT TOP 10 FROM VWTenantPropertiesResults ORDER BY ContentPrice"

    'display the results
    if myRecordSet2.BOF then
    do until myRecordSet2.EOF
    %>
    <div class='result'>")
      <dl><%=myRecordSet2("ContentTitle")%></dl>
      <dt><%=myRecordSet2("ContentStreet")%></dt>
      <dt><%=myRecordSet2("ContentTown")%></dt>
      <dt><%=myRecordSet2("ContentPostcode")%></dt>
    </div><%
    myRecordSet2.MoveNext
    loop
end if

end if
%>

</body>
</html>

但是当我查看该页面时,我收到以下错误;

ADODB.Recordset error '800a0e78'

Operation is not allowed when the object is closed.

/welcome/gist188770.asp, line 98 

第98行如下;

if myRecordSet2.BOF then

我想知道是否有人可以帮我解决这个问题?感谢您提供的任何帮助。

尼尔。

1 个答案:

答案 0 :(得分:2)

您尚未使用BOF打开记录集。

此外,您正在创建一个命令对象,但没有为其分配SQL文本,而是将sql文本直接分配给记录集。

您可以将SQL分配给命令对象的CommandText属性,然后通过调用Command对象Execute方法检索记录集。

修改

需要修复的更多内容

您的SQL concatentation在每行末尾不包含任何vbCrLf或空格。 SQL将不会执行。

不要将连接字符串直接分配给ActiveConnection,虽然这样做有效ADODB不能正确地管理连接池。始终实例化Connection对象并将其分配给ActiveConnection属性。

编辑2

又一个问题

您的代码正在访问Request.QueryString中的条件,但您的form元素指定了method="post",在这种情况下您应该使用Request.Form代替。