BrainDump
  Home arrow BrainDump arrow Page 3 - ADO Queries and Working with Recordsets
Click Here
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

ADO Queries and Working with Recordsets
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-01-14

    Table of Contents:
  • ADO Queries and Working with Recordsets
  • Displaying Selected Records Using a Drop Down Menu
  • Sorting Records
  • How to Add a Record to Table
  • How To Delete a Record

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    ADO Queries and Working with Recordsets - Sorting Records


    (Page 3 of 5 )

    It is possible to sort recordsets in ADO by using the SQL ORDER BY function. Here, we will sort our data by VictimName in ascending order:


    <html>
    <body>

    <%
    set conn=Server.CreateObject("ADODB.Connection")
    conn.Provider="Microsoft.Jet.OLEDB.4.0"
    conn.Open(Server.Mappath("/db/chucknorrisvictims.mdb"))
    set rs = Server.CreateObject("ADODB.recordset")
    sql="SELECT VictimName FROM Victims ORDER BY VictimName"
    rs.Open sql, conn
    %>


    <table border="1" width="100%">
    <tr>
    <%for each x in rs.Fields
    response.write("<th>" & x.name & "</th>")
    next%>

    </tr>
    <%do until rs.EOF%>
    <tr>
    <%for each x in rs.Fields%>
    <td><%Response.Write(x.value)%></td>
    <%next
    rs.MoveNext%>

    </tr>
    <%loop
    rs.close
    conn.close
    %>

    </table>

    </body>
    </html>

    In the above code we tell the program to select all of the data in the VictimName column and sort in ascending order by VictimName. The program then opens up the database connection, writes each field by looping through the values until it comes to the end of the file, and then closes the connection. The result would be:

     

    VictimName

    Death

    Jackie Chan

    Ralph Macchio

    To sort by Descending, you would do the same thing, except you would add DESC to the end of the SQL Select statement:


    <html>
    <body>

    <%
    set conn=Server.CreateObject("ADODB.Connection")
    conn.Provider="Microsoft.Jet.OLEDB.4.0"
    conn.Open(Server.Mappath("/db/chucknorrisvictims.mdb"))
    set rs = Server.CreateObject("ADODB.recordset")
    sql="SELECT VictimName FROM Victims ORDER BY VictimName"
    rs.Open sql, conn
    %>


    <table border="1" width="100%">
    <tr>
    <%for each x in rs.Fields
    response.write("<th>" & x.name & "</th>")
    next%>

    </tr>
    <%do until rs.EOF%>
    <tr>
    <%for each x in rs.Fields%>
    <td><%Response.Write(x.value)%></td>
    <%next
    rs.MoveNext%>

    </tr>
    <%loop
    rs.close
    conn.close
    %>

    </table>

    </body>
    </html>

    Which of course would result in:

     

    VictimName

    Ralph Macchio

    Jackie Chan

    Death

    We could also allow the user to select which column they would like to sort on as well:


    <html>
    <body>

    <table border="1" width="100%">
    <tr>
    <th align="left">
    <a href="sample.asp?sort=victimname">Victim Name</a>
    </th>
    <th align="left">
    <a href="sample.asp?sort=victiminjury">Victim Injury</a>
    </th>
    </tr>
    <%
    if request.querystring("sort")<>"" then
    sort=request.querystring("sort")
    else
    sort="victimname"
    end if

    set conn=Server.CreateObject("ADODB.Connection")
    conn.Provider="Microsoft.Jet.OLEDB.4.0"
    conn.Open(Server.Mappath("/db/chucknorrisvictims.mdb"))
    set rs=Server.CreateObject("ADODB.recordset")
    sql="SELECT VictimName,VictimInjury FROM Victims ORDER BY " & sort
    rs.Open sql,conn

    do until rs.EOF
    response.write("<tr>")
    for each x in rs.Fields
    response.write("<td>" & x.value & "</td>")
    next
    rs.MoveNext
    response.write("</tr>")
    loop
    rs.close
    conn.close
    %>

    </table>

    </body>
    </html>

    The above code creates a table consisting of the VictimName and VictimInjury columns. Each column header has a link in it. If the user clicks on either link, it will sort the table by that column.

    More BrainDump Articles
    More By James Payne


       · Welcome to my article on ADO Queries. Prepare to have your puny mind blown as you...
       · Hi James, I'd like to see an article dealing with how to import attribute-centric...
     

    BRAINDUMP ARTICLES

    - Codes and Packages in Microsoft Project 2007
    - Windows 7: Rumors and Demos
    - XP SP3 Why Me?
    - Breaking Up Your Work in Microsoft Project
    - Breaking Work into Task-Sized Chunks
    - Putting Microsoft`s Worldwide Telescope Unde...
    - Handling Multiple Contracts with Indigo
    - Cleaning Out Your Data in XP
    - Multiple Service Contracts and Indigo
    - Cleaning Out Your Programs in XP
    - Handling Metadata with Indigo
    - Building Blocks for a WCF Service Web Site
    - Help! I Need Some Remote Assistance
    - Using Service Templates with Indigo
    - Windows XP Tips for Task Manager

    Iron Speed




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway