Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Query

SQL Query

Scheduled Pinned Locked Moved Database
databasesysadminalgorithmsregexhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tyrone_whitey
    wrote on last edited by
    #1

    I have a database that a user can search for using the begins with, exact match, or contains option. I did a request.querystring to pull in that information from the previous page where the user selects one of those options and types is what he is searching for. The problem is that I cannot figure out how to display the users result when they select to search by exact match, begins with, or contains. The following is the code that I used. Can anyone tell me what is wrong with it and give me some leads on how to get it fixed. Table Display <%Dim FieldHead, SearchMethod, TextBox, Table, DB, RS, strSql%> <%'Request the information from the previous page'%> <%FieldHead = Request.QueryString("fieldhead") SearchMethod = Request.QueryString("SrchMthd") TextBox = Request.QueryString("textbox") Table = Request.QueryString("Table") %> <%'search method if the user selects begins with%> <%If SearchMethod = "begin" Then%> <%Set DB = Server.CreateObject("ADODB.Connection") DB.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE= C:\Inetpub\wwwroot\ASP\ThomasTate\North.mdb") Set RS = Server.CreateObject("ADODB.Recordset") strSql = "SELECT * FROM " & Table " WHERE " & FieldHead & " LIKE '" & TextBox & "%'" RS.Open (strSql), DB End IF %> <%'search method if the user selects contains%> <%If SearchMethod = "contain" Then%> <%Set DB = Server.CreateObject("ADODB.Connection") DB.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE= C:\Inetpub\wwwroot\ASP\ThomasTate\North.mdb") Set RS = Server.CreateObject("ADODB.Recordset") strSql = "SELECT * FROM " & Table " WHERE " & FieldHead & " LIKE '%" & TextBox & "%'" RS.Open (strSql), DB End IF%> <%'search method if the user selects exact match%> <%If SearchMethod = "exact" Then%> <%Set DB = Server.CreateObject("ADODB.Connection") DB.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE= C:\Inetpub\wwwroot\ASP\ThomasTate\North.mdb") Set RS = Server.CreateObject("ADODB.Recordset") strSql = "SELECT * FROM " & Table " WHERE " & FieldHead & = TextBox RS.Open (strSql), DB End IF%>

    Ty

    A 1 Reply Last reply
    0
    • T Tyrone_whitey

      I have a database that a user can search for using the begins with, exact match, or contains option. I did a request.querystring to pull in that information from the previous page where the user selects one of those options and types is what he is searching for. The problem is that I cannot figure out how to display the users result when they select to search by exact match, begins with, or contains. The following is the code that I used. Can anyone tell me what is wrong with it and give me some leads on how to get it fixed. Table Display <%Dim FieldHead, SearchMethod, TextBox, Table, DB, RS, strSql%> <%'Request the information from the previous page'%> <%FieldHead = Request.QueryString("fieldhead") SearchMethod = Request.QueryString("SrchMthd") TextBox = Request.QueryString("textbox") Table = Request.QueryString("Table") %> <%'search method if the user selects begins with%> <%If SearchMethod = "begin" Then%> <%Set DB = Server.CreateObject("ADODB.Connection") DB.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE= C:\Inetpub\wwwroot\ASP\ThomasTate\North.mdb") Set RS = Server.CreateObject("ADODB.Recordset") strSql = "SELECT * FROM " & Table " WHERE " & FieldHead & " LIKE '" & TextBox & "%'" RS.Open (strSql), DB End IF %> <%'search method if the user selects contains%> <%If SearchMethod = "contain" Then%> <%Set DB = Server.CreateObject("ADODB.Connection") DB.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE= C:\Inetpub\wwwroot\ASP\ThomasTate\North.mdb") Set RS = Server.CreateObject("ADODB.Recordset") strSql = "SELECT * FROM " & Table " WHERE " & FieldHead & " LIKE '%" & TextBox & "%'" RS.Open (strSql), DB End IF%> <%'search method if the user selects exact match%> <%If SearchMethod = "exact" Then%> <%Set DB = Server.CreateObject("ADODB.Connection") DB.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE= C:\Inetpub\wwwroot\ASP\ThomasTate\North.mdb") Set RS = Server.CreateObject("ADODB.Recordset") strSql = "SELECT * FROM " & Table " WHERE " & FieldHead & = TextBox RS.Open (strSql), DB End IF%>

      Ty

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      Your code should use:

      ... " WHERE " & FieldHead & " LIKE '" & Replace(TextBox, "'", "''") & "%'
      

      Instead of:

      ... " WHERE " & FieldHead & " LIKE '" & TextBox & "%'"
      

      Otherwise it will break if the user enters a single-quote into his search value. The normal way to show the results is to iterate through each row and column in the result set. However, you might find the following faster:

      'Open the recordset and render the data in HTML format.
      For Each objField In objRS.Fields
         Call Response.Write("<td nowrap><b>" & _
               Server.HtmlEncode(objField.Name) & "</b></td>" & vbCRLF)
      Next
      Call Response.Write("</tr>" & vbCRLF)
      Call Response.Write("<tr>")
      If Not objRs.eof Then
         strData = objRS.GetString( , , "#<#/td#>##<#td#>#", _
               "#<#/td#>##<#/tr#>##<#tr#>##<#td nowrap#>#", "#&#nbsp;")
         strData = Replace(Replace(Replace(strData, "#&#", Chr(1)), "&", "&amp;"), Chr(1), "&")
         strData = Replace(Replace(Replace(strData, "#<#", Chr(1)), "<", "&lt;"), Chr(1), "<")
         strData = Replace(Replace(Replace(strData, "#>#", Chr(1)), ">", "&gt;"), Chr(1), ">")
         strData = "<td>" & Replace(Left(strData, Len(strData) - 15), "</tr>", "</tr>" & vbCRLF)
         Call Response.Write(strData)
      Else
         Call Response.Write("<td colspan=""" & objRs.Fields.Count & """>No records found.</td></tr>" & vbCRLF)
      End If
      Call Response.Write("</table>" & vbCRLF)
      

      The funny replace stuff is a quick way to get it to render each field as a separate HTML table cell. Andy

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups