Shiv Kumar
 Hobbyist Filmmaker / Editor

Sending ADO Recordsets to an ASP Page

Not Rated YetNot Rated YetNot Rated YetNot Rated YetNot Rated Yet0votes
January 19, 2008 11:06 AM  Views: 1359   Favorited: 0 Favorite It Comments: 2
Filed Under:  Programming
Tags:  ADO, ASP, COM, Delphi
 
This project's output is similar to the previous project. But the difference here is that our ASP object sends back an ADO Recordset object and the actual generation of HTML (layout) is done in ASP script. This example is very typical of what most ASP pages are like. The grunt of the work is done in interpreted script rather than as a method of a compiled object.

One of the visitors to my site (Doug Bowman) had asked me if Delphi could send back a Recordset object, saying that this was quite easily done in VB. That statement got me real curious to say the least. But, since I didn't know what he meant, I wasn't sure what he was asking. So I in turn posed this question to Ronaldo Melo Ferraz (those of you who frequent the Borland news groups would know him as one of the top rankers, answering most of the questions, giving in depth answers, links and other valuable information). Ronaldo does some work with ASP and came up with a Delphi solution soon enough. This helped me getting started with this tutorial. I'd like to thank Doug for coming up with the "problem" and Ronaldo for giving me a solution.

I've since modified Ronaldo's code a bit to fit this tutorial as well as Doug's ASP script to change the output to look like that of the previous tutorial's. The similarity of the output should give people a good idea of how to do things in the "typical" ASP way and the "Delphi way".

The ASP page that instantiates our ASP object and gets it to return an ADO Recordset looks like this:

<html>
<head>
<LINK REL=STYLESHEET TYPE="text/css" HREF="http://www.matlus.com/home-styles.css">
</head>
<body>
<% Dim ASPObj %>
<% Dim rs %>
<% set ASPObj = Server.CreateObject("DelphiADO.Recordset") %>
<% set rs = ASPObj.ReturnADO %>
<Table Cellspacing="1" Cellpadding="1" Border="1">
  <tr>
  <% For j = 0 to rs.fields.count - 1 %>
    <td align="center">
      <b><% Response.Write(rs.fields(j).name) %></b>
    </td>
  <% Next %>
  </tr>
<%Do Until rs.Eof %>
  <tr>
  <% For j = 0 to rs.fields.count - 1 %>
    <td>
      <% Response.Write(rs.fields(j)) %>
    </td>
  <% Next %>
  </tr>
<%  rs.movenext %>
<%Loop%>
</Table>
<% set ASPObj = nothing %>
<% set rs = nothing %>
</body>
</html>
Looking at the ASP script above you should notice a few things immediately. DelphiADO is the name of the COM DLL that contains our ASP object (this DLL needs to be registered on the target machine as described in the previous tutorial). The object, in this case is called Recordset. The one and only method of this object is ReturnADO which returns an ADO Recordset object to the calling ASP script. This ASP script then goes about "laying out" the recordset using the (ASP) Response object's Write method. This is similar to what we did inside our ASP object in the previous tutorial. Remember that this ASP script is being processed at the server end and so visitors to this .asp file will not see the actual ASP script, but rather the generated HTML. This is typical of any server side technology (ISAPI/CGI included).

What's Required

Apart from what was mentioned in the previous tutorial for ASP per se, we will need an ADO compliant database for this project. What I've done is I've migrated the Vendors table from DBDEMOS to an Access 97 database using the Data Pump utility that ships as a part of Delphi. This database is not included with the project download. However, Borland actually ships the equivalent of DBDEMOS as an Access database solely for the purpose of giving us Delphi programmers an ADO compliant database as a test database. I had deleted my copy of this database and so had to re-create at least the Vendors table for this tutorial.

Getting started

  1. Select File | New
  2. Switch to the ActiveX page of the New item dialog
  3. Select ActiveX Library
  4. Click on the OK Button
This action will generate the skeleton for a COM DLL.
  1. Change the output directory to your web server's scripts folder (C:\inetpub\scripts)
  2. Save this Project as DelphiADO
  3. From the File menu, select New
  4. Swtich to the ActiveX page again
  5. Choose ASPObject
  6. Click OK
  7. In the dialog box presented, fill out the fields as shown in Figure 1 and click OK

Figure 1 Showing the values that need to be filled/selected

If you are running IIS 4.0 leave the Active Server Type in its default setting. For IIS 5.0 and above, choose the option as shown in Figure 1.

You should see the .asp page that Delphi generates for us as well as the Type Library Editor and the unit that implements the IRecordset interface. Delphi generates the following code for us:

unit Unit1;
interface
uses
  ComObj, ActiveX, AspTlb, Project1_TLB, StdVcl;
type
  TRecordset = class(TASPMTSObject, IRecordset)
  end;
implementation
uses ComServ;
initialization
  TAutoObjectFactory.Create(ComServer, TRecordset, Class_Recordset,
    ciMultiInstance, tmApartment);
end.
You will notice that our object TRecordset has no methods as yet. Specifically, we need a method called ReturnADO.

As usual, we need to use the Type Library Editor to add methods to our ASP object.

  1. In the Type Library editor, select the IRecordset interface and add a new method by either right clicking and choosing New Method or clicking on the New Method icon on the tool bar
  2. Name this method - ReturnADO
  3. Swtich to the Parameters tab on the right
  4. Add a parameter for this method such that your Type library editor looks like that shown in Figure 2
  5. Once done, click on the Refresh Implementation tool bar button

Figure 2 showing the new method and its parameters in the Type Library editor

Refreshing the implementation of the ASP object (in the type library editor) will generate the method declaration in our TRecordset object for the method we just added to the IRecordset interface. The interface section of the TRecordset object should look like that shown below. In particular, make sure the declaration of the ReturnADO method in your project looks like that shown here.

interface
uses
  ComObj, ActiveX, AspTlb, DelphiADO_TLB, StdVcl, ADOdb, Dbtables;
type
  TRecordset = class(TASPMTSObject, IRecordset)
  protected
    function ReturnADO: OleVariant; safecall;
  end;
It's now time for us to save our work thus far. Unit1 should be saved as RecordsetImpl. We will change the script in the .asp file later, for now save it as it is.

Next, we will implement the ReturnADO method of our TRecordset. This part is really simple as we once again come back to Delphi programming. We aren't using a DataModule in this project like we did in the last one. There is no specific reason for this expect that I wanted to keep this project simple. Since we don't have a DataModule here we need to create an instance of a TADODataset on the fly, assign the required properties and then return the Recordset as the result of this function. The RecordsetImpl unit's listing is shown below:

unit RecordsetImpl;
interface
uses
  ComObj, ActiveX, AspTlb, DelphiADO_TLB, StdVcl, ADOdb;
type
  TRecordset = class(TASPMTSObject, IRecordset)
  protected
    function ReturnADO: OleVariant; safecall;
  end;
implementation
uses ComServ;
function TRecordset.ReturnADO: OleVariant;
var
  ADODataSet1 : TADODataSet;
  RecordSet   : _Recordset;
begin
  ADODataSet1 := TADODataSet.Create(nil);
  try
    ADODataSet1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Common Files\Borland Shared\Data\dbdemos.mdb;Persist Security Info=False';
    ADODataSet1.CommandText := 'SELECT * FROM VENDORS';
    ADODataSet1.Open;
    RecordSet := ADODataSet1.Recordset._xClone;
    RecordSet.Set_ActiveConnection(nil);
    Result := RecordSet;
    ADODataSet1.Close;
  finally
    ADODataSet1.Free;
  end;
end;
initialization
  TAutoObjectFactory.Create(ComServer, TRecordset, Class_Recordset,
    ciMultiInstance, tmApartment);
end.
Be sure to add the ADOdb unit to the uses clause of the interface section of the unit. To be Continued

The complete project is available for download from the link below.

Comments have been Disabled for this post





Leave A Comment

Shiv Kumar
Gainesville, Virginia,
United States
Member Bio Member Skills/Specialization

Bio

close

Specializations

close
Photographer
Landscape
Nature
Portrait
Videographer/Cinematographer
Interview
Landscape
Nature
Portrait
 
Privacy Policy | Terms Of Service | Contact Us | Support | Help/FAQ | News