|
Display RecordSet Contents
By Steven Smith
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>" & 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
Function_Exit:
Exit Function
ErrorHandler:
objContext.SetAbort
Err.Raise Err.Number, Err.Source, "ListColumnists: " & Err.Description
Resume Function_Exit
End Function
|
The source code for the example is:
1 <% OPTION EXPLICIT %> 2 <!-- #INCLUDE VIRTUAL="/stevesmith/include/articleformat.asp" --> 3 <% 4 'Declare Variables 5 Dim objAspAllianceRead 6 Dim objLib 7 Dim objAspAllianceWeb 8 Dim rs 9 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 %>
|
|
|