Display RecordSet Contents
ASPAlliance.com: The #1 ASP.NET Community
The ASPSmith
Search
D: | Domains | Authors.aspalliance.com | Stevesmith | Articles | Display RecordSet Contents
Display RecordSet Contents

By Steven Smith

[Example]

Frequently when debugging you'll simply want to view the contents of a recordset on an ASP page. For these special moments, I've written a simple function that does this for me and allows me to quickly set a few common parameters. I've even gone so far as to add the function to a COM object to make it more accessible than an include file. Here is the code in either case (ASP or VB6):

ASP Code:
'This function displays a recordset's contents in a table
'asColumns determines whether the data is in rows or columns. If False, then only one record is displayed, with the
' recordset headings in the left column and the data in the right column.
'heading is text to display in the table's caption.
'table_options are added to the <table> tag
'header_options are added to the recordset heading tags
'datacell_options are added to the recordset data tags
Function DisplayRecordsetContents(byval rs, ByVal asColumns, ByVal heading, byval table_options, byval header_options, byval datacell_options)
     Dim strOutput
     Dim I
     If Not rs.EOF Then
          strOutput = "<table " & table_options & "><caption>" & heading & "</caption>" & chr(10)
          If asColumns Then
               strOutput = strOutput & "<tr>" & chr(10)
               For I = 0 To rs.Fields.Count -1
                    strOutput = strOutput & "<th " & header_options & ">" & rs.Fields(I).Name & "</th>" & chr(10)
               Next
               strOutput = strOutput & "</tr>" & chr(10)
               While Not rs.EOF
                    strOutput = strOutput & "<tr>" & chr(10)
                    For I = 0 To rs.Fields.Count -1
                         strOutput = strOutput & "<td " & datacell_options & ">" & rs.Fields(I).Value & "</td>" & chr(10)
                    Next
                    strOutput = strOutput & "</tr>" & chr(10)
                    rs.MoveNext
               Wend
               strOutput = strOutput & "</table>" & chr(10)
          Else
               For I = 0 To rs.Fields.Count
                    strOutput = strOutput & "<tr>" & chr(10)
                    strOutput = strOutput & "<td " & header_options & ">" & rs.Fields(I).Name & "</td>" & chr(10)
                    strOutput = strOutput & "<td " & datacell_options & ">" & rs.Fields(I).Value & "</td>" & chr(10)
                    strOutput = strOutput & "</tr>" & chr(10)
               Next
               strOutput = strOutput & "</table>" & chr(10)
          End If
     Else
          strOutput = heading & "<br>No records to display."
     End If

     DisplayRecordsetContents = strOutput
End Function

VB6 COM+ Code:
'VB6 COM+ Function
'Public
Function DisplayRecordsetContents(ByVal rs As Recordset, _
                                                   ByVal asColumns As Variant, _
                                                   Optional ByVal heading As Variant, _
                                                   Optional ByVal table_options As Variant, _
                                                   Optional ByVal header_options As Variant, _
                                                   Optional ByVal datacell_options As Variant) As String
     On Error GoTo ErrorHandler
     Dim strOutput As String
     Dim I As Long
     
     'Validate Params (paramclean just checks to see if value is null and if so replaces it with second value)
     asColumns = ParamClean(asColumns, True, False)
     heading = ParamClean(heading, "", False)
     table_options = ParamClean(table_options, "", False)
     header_options = ParamClean(header_options, "", False)
     datacell_options = ParamClean(datacell_options, "", False)
     
     If Not rs.EOF Then
          strOutput = "<TABLE ? & table_options><CAPTION>" &amp; heading &amp; "</CAPTION>" &amp; Chr(10)
          If asColumns Then
               strOutput = strOutput &amp; "<TR>" &amp; Chr(10)
               For I = 0 To rs.Fields.Count - 1
                    strOutput = strOutput &amp; "<TH ? & header_options>" &amp; rs.Fields(I).Name &amp; "</TH>" &amp; Chr(10)
               Next
               strOutput = strOutput &amp; "</TR>" &amp; Chr(10)
               While Not rs.EOF
                    strOutput = strOutput &amp; "<TR>" &amp; Chr(10)
                    For I = 0 To rs.Fields.Count - 1
                         strOutput = strOutput &amp; "<TD ? & datacell_options>" &amp; rs.Fields(I).Value &amp; "</TD>" &amp; Chr(10)
                    Next
                    strOutput = strOutput &amp; "</TR>" &amp; Chr(10)
                    rs.MoveNext
               Wend
               strOutput = strOutput &amp; "</TABLE>" &amp; Chr(10)
          Else
               For I = 0 To rs.Fields.Count
                    strOutput = strOutput &amp; "<TR>" &amp; Chr(10)
                    strOutput = strOutput &amp; "<TD ? & header_options>" &amp; rs.Fields(I).Name &amp; "</TD>" &amp; Chr(10)
                    strOutput = strOutput &amp; "<TD ? & datacell_options>" &amp; rs.Fields(I).Value &amp; "</TD>" &amp; Chr(10)
                    strOutput = strOutput &amp; "</TR>" &amp; Chr(10)
               Next
               strOutput = strOutput &amp; "</TABLE>" &amp; Chr(10)
          End If
     Else
          strOutput = heading &amp; "<BR>No records to display."
     End If
     DisplayRecordsetContents = strOutput
Function_Exit:
     Exit Function
ErrorHandler:
     objContext.SetAbort
     Err.Raise Err.Number, Err.Source, "ListColumnists: " &amp; Err.Description
     Resume Function_Exit
End Function

The source code for the example is:
   <% OPTION EXPLICIT %>
   <!-- #INCLUDE VIRTUAL="/stevesmith/include/articleformat.asp" -->
   <%
   'Declare Variables
   Dim objAspAllianceRead
   Dim objLib
   Dim objAspAllianceWeb
   Dim rs
   
10   Set objAspAllianceRead = Server.CreateObject("AspAlliance.Read")
11   Set objAspAllianceWeb = Server.CreateObject("AspAlliance.WebClass")
12   Set objLib = Server.CreateObject("SSLibrary.Library")
13   Set rs = Server.CreateObject("ADODB.Recordset")
14   
15   Call ArticleHeader("Display Recordset Example","","")
16   
17   Set rs = objAspAllianceRead.ListArticlesForColumnist(1)
18   Response.Write objLib.DisplayRecordsetContents(rs,True,"Steve's Articles","border=""1"" bgcolor=""#EEEEEE""", "bgcolor=""#CCCCCC""", "")
19   
20   Call ArticleFooter()
21   %>





ASP.NET Developer's Cookbook, By Steven Smith, Rob Howard, ASPAlliance.com 

ASP.NET By Example, By Steven Smith 




Steven Smith, MCSE + Internet (4.0)
Last Modified: 7/26/2001 9:10:56 PM
History: 1/25/2004 6:10:04 PM