GetROWS and request.querystring

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jdom
    New Member
    • Sep 2008
    • 10

    GetROWS and request.querystring

    have this code but not sure how i can use querystring from another search form page to define the CAT_ID = ? AND MAKE = ?
    Thank you

    Code:
    <%@ Language=VBScript %>
    <% Option Explicit %>
    <%
    
    
    Dim objCN ' ADO Connection object
    Dim objRS ' ADO Recordset object
    Dim strsql ' SQL query string
    Dim RecordsArray ' To hold the Array returned by GetRows
    Dim i ' A counter variable
    
    ' Create a connection object
    Set objCN = Server.CreateObject("ADODB.Connection")
    
    ' Connect to the data source
    objCN.ConnectionString = "DATABASECONNECTION"
    objCN.Open
    
    ' Prepare a SQL query string
    strsql = "SELECT* FROM ADS WHERE CAT_ID = ? AND MAKE = ? ORDER BY  CAT_ID DESC"
    
    ' Execute the SQL query and set the implicitly created recordset
    Set objRS = objCN.Execute(strsql)
    
    
    ' Write out the results using GetRows in a loop
    Response.write "<pre>"
    Do While Not objRS.EOF
        RecordsArray = objRS.GetRows(30)
        
        ' Print out the array
        For i = 0 To UBound(RecordsArray, 2)
            Response.write RecordsArray(0, i)
            Response.write vbTab
            Response.write RecordsArray(1, i)
            Response.write vbTab
            Response.write RecordsArray(2, i)
            Response.write vbTab
            Response.write RecordsArray(3, i)
            Response.write vbTab
            Response.write vbCrLf
        Next
    Loop
    Response.write "</pre>"
    
    objRS.Close
    objCN.Close
    Set objCN = Nothing
    Set objRS = Nothing
    
    %>
    Last edited by DrBunchman; Sep 24 '08, 07:34 AM. Reason: Added [Code] Tags - Please use the '#' button
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi jdom,

    You need to retreive the QueryString values and add them to your sql string. You can do it like this:

    Code:
    strsql = "SELECT* FROM ADS WHERE CAT_ID = " & Request("catid") & " AND MAKE = " & Request("makeid") & " ORDER BY  CAT_ID DESC"
    Or you can use parameters, which is the more correct method but I can't recall off the top of my head what the syntax is! It's something like:

    Code:
    objCN.Parameters.Add(Request("catid") )
    But I'd suggest having a quick Google to check it out.

    Let me know how it goes.

    Dr B

    Comment

    • jdom
      New Member
      • Sep 2008
      • 10

      #3
      Subscript out of range

      This new question can't find where to post a new one. anyway

      i have this code that is giving me a seriouse of error:
      Subscript out of range: '-40'
      line : strid = aResults(0, iRowLoop)

      <% objDBConnection .Open "DATABASE"
      set objDBCommand = Server.CreateOb ject("ADODB.Com mand")
      objDBCommand.Ac tiveConnection = objDBConnection
      objDBCommand.Co mmandText = "SELECT AD_ID,make, model from cars "& tmpOrderBy


      Dim iRows, iCols, iRowLoop, iColLoop, iStop
      iRows = UBound(aResults , 2)

      If iRows > (iOffset + iStart) Then
      iStop = iOffset + iStart - 1
      Else
      iStop = iRows
      End If

      For iRowLoop = iStart to iStop
      'strcheckbox = aResults(0, iRowLoop)
      strid = aResults(0, iRowLoop)
      smake = aResults(1, iRowLoop)
      smodel = aResults(3, iRowLoop)
      Response.Write "<input type='checkbox' name='compare' id='compare' value="& strid &">"

      Next
      Response.Write "<div align=""center" ">"
      if iStop < iRows then
      'Show Next link
      Request.ServerV ariables("SCRIP T_NAME")
      Response.Write " <A HREF=""?Page=" & iStart-iOffset & _
      "&Offset=" & iOffset & """>"
      ' Response.Write
      Response.Write "<<< " & iOffset & "</A>"
      ' end if

      end if
      if iStop < iRows then
      'Show Next link
      Request.ServerV ariables("SCRIP T_NAME")
      Response.Write " <A HREF=""?Page=" & iStart+iOffset & _
      "&Offset=" & iOffset &">"
      ' Response.Write
      Response.Write " >>> " & iOffset & "</A>"
      Response.Write Space(4)
      ' end if&nbsp;

      end if %>

      Please help thank you

      Comment

      • jdom
        New Member
        • Sep 2008
        • 10

        #4
        Anyone has any suggestion please

        Comment

        • Nicodemas
          Recognized Expert New Member
          • Nov 2007
          • 164

          #5
          From the code you posted, I do not see where aResults becomes a 2d array. Also, there cannot be a negative index in an array. Check the value of iRowLoop, iStart, and iStop using response.write' s somewhere before you begin your For...Next loop. Make sure the values assigned to those variables are not negative and are correct to your assumed test values.

          Side note --- in your first post I noticed you have a Do While...Loop in your code (refer to lines 28 and 43). That loop is unnecessary because you are not iterating through anything. Use a if...then statement instead. It'll make your code more efficient and easier to understand.

          Also, I am not 100% positive of this, but shouldn't that Do While....Loop create an infinite loop bug? I do not think GetRows moves the cursor to the EOF marker so it should cycle indefinitely. Could be wrong... but definitely not wrong about using If...Then instead of the loop.

          Comment

          Working...