Creating a Datagrid Class in classic ASP

Using the Class in an asp page

Paste the class code into notepad and save it as caDataGrid.asp in your wwwroot folder. I use the nwind.mdb database as an example here which is found with plenty of MS products, in my case in my Visual Basic folder. I copied the database to the wwwroot folder for simplicity. Now create an asp page in your wwwroot folder with the following:-

<%@ LANGUAGE="VBSCRIPT" %>
<% option explicit %>
<% response.buffer=true %>
<html>
<head>
   <title>caDataGrid Test</title>
</head>
<!-- #include file="caDataGrid.asp"-->
<body>
<%
dim cDataGrid, mapPath
Set cDataGrid = New caDataGrid
mapPath = "nwind.mdb"
'Access 2000 connection
cDataGrid.ConnectionString =  "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="& Server.MapPath(mapPath)
cDataGrid.SqlString =  "select firstname, lastname, title from employees"
cDataGrid.Bind
set cDataGrid = nothing

'now lets set our own columns
response.write "<br><br>"
Set cDataGrid = New caDataGrid
cDataGrid.ConnectionString =  "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="& Server.MapPath(mapPath)
cDataGrid.SqlString =  "select firstname, lastname, title from employees"
cDataGrid.AutoColumns = false
cDataGrid.AddColumn("First Name")
cDataGrid.AddColumn("Last Name")
cDataGrid.AddColumn("Job Title")
cDataGrid.Bind
set cDataGrid = nothing
%>
</body>
</html>


You 'reference' the class using the include capability of asp. Of course theres nothing to say you can't just paste your class code into the same page but it wouldn't be nice and neat would it? Then you declare a variable and set it as a caDataGrid object/class, assign the relevent values and bind.  All of 5/6 lines of code. Very handy.

If you have been paying attention you might have realised how much more you could do with this. First on the list should be better error code. For example if I set cDataGrid.AutoColumns = false and then forget to add column names then I'd have get bad output. On the more interesting side of things I could add alot of similar stuff to this class as you find with the .net datagrid - paging, background colours or even class names from a stylesheet all with some more properties and methods. More work obviously but once it's done it'll be there to use many many times. Until tomorrow when you install the .net framework and forget all about it. Anyway hope this is of use to you all. Happy Coding.

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.

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” - Tom Cargill