We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 71,959 times

Contents

Related Categories

Creating a Datagrid Class in classic ASP - 2-Dimensional Arrays

musician

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.

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 application must keep it's 3 most important aspects seperate - presentation (external CSS), structure (XHTML) and behavior (external javascript).

Comments

  • Used this class as a base for a DropDownMenu class

    Posted by dibley1973 on 05 Aug 2006

    What a great article!!! I have just been starting to use .Net at work but still use classic ASP for home projects so liked the similarity of a near OO approach. I needed a DropDown...

  • formatting the content of an automatically generat

    Posted by trish on 11 Aug 2005

    Did you ever find out on how to do this..........? I'm try to accomplish the same...

    Thanks

  • Quick Improvement Tip...

    Posted by ChristianCalderon on 05 Apr 2004

    When looping and printing from a recordset it is better to get a reference to the fields that we want to print.
    For instance, this code would be more efficient (specially true when printing large set...

  • formatting the content of an automatically generat

    Posted by lotiejam on 26 Feb 2003

    I have used the datagrid class code to great success but would like to format one of the columns so the data displayed is can be used as a link(url). Each url would point to the same page and session ...

  • Datagrid class

    Posted by moose on 04 Jul 2002

    I thought the class was groovy. It will save a lot of time setting up data driven pages.

    Well done mate!

    I've ammended the class to parameterise the table formatting. Very easy to do.
    Thanks fo...