Creating a Datagrid Class in classic ASP

2-Dimensional Arrays

Even more efficient is to use the GetRows method of the Recordset to populate a 2 dimensional array. Then you no longer have to use up resources accessing the Recordset.

dim arTable
arTable = objRec.GetRows
objRec.Close
set objRec = nothing


You now have a 2 dimensional array but how is the recordset information stored in this array?

arTable's 2 dimensions are like this: arTable(TotalCols, TotalRows). So if my Recordset returned 10 records of the above fields it would be arTable(3, 10) and you would need to go from arTable(0, 0) to arTable(2, 9) to retreive each value and you must go through the TotalCols elements first, followed by the TotalRows element. You don't know how many records you will return so you need to use the UBound function to evaluate them:-

dim tCols, tRows
tCols = UBound(arTable, 1)
tRows = UBound(arTable, 2)


With an array of more than one dimension you need to supply the UBound function with a value to indicate which element you wish to total. As above we set tCols to the total of the first dimension and tRows to the total of the second dimension.

With this information you can now loop through the records using nested For Next loops:-

<table>
<%
Dim x, y
For x = 0 to tRows
   Response.Write "<tr>"
   For y = 0 to tCols
       Response.Write "<td>" & rTable(y, x) & "</td>"
   Next
   Response.Write "</tr>"
Next
%>


Not as simple as directly referring to field names in the recordset object but faster and the code is hardly more complicated. So now wehave a good model which we can move/encapsulate in a handy vbscript class.

You might also like...

Comments

About the author

Brian O'Connell Ireland

Microsoft Certified Applications Developer with 10 years experience developing web based applications using asp, asp.net for a Local Authority in Dublin. Clings to a firm belief that a web appli...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There's no test like production” - Anon