How To Use an ASP Page as a Data Source for an RDS Data Control (259533)



The information in this article applies to:

  • ActiveX Data Objects (ADO) 2.5
  • ActiveX Data Objects (ADO) 2.6
  • ActiveX Data Objects (ADO) 2.7
  • Microsoft Active Server Pages

This article was previously published under Q259533

SUMMARY

In ADO versions 2.0 and 2.1, the Remote Data Service (RDS) data control could only be bound to a data source by specifying appropriate values for its Server, Connect and SQL parameters. ADO 2.5 and later incorporate a URL parameter for the RDS data control. The URL parameter makes it possible to bind the control to an ASP page that persists an ADO recordset to the ASP Response stream.

MORE INFORMATION

Using the URL Parameter of the RDS Data Control in ADO 2.5

The ASP page connects to the SQL Server 7.0 PUBS sample database and opens an ADO recordset on the Authors table. The recordset is then persisted to the ASP Response stream in XML format. Follow the steps listed below to create the ASP page:
  1. Open Notepad.
  2. Copy and paste the following code into a new file:

    Note You must change User Id=<username> to the correct value before you run this code. Make sure that the User Id has the appropriate permissions to perform this operation on the database.
    <!-- #include file='adovbs.inc' -->
    <%
       Dim cn
       Dim rs
       Set cn = Server.CreateObject("ADODB.Connection")
       cn.Open "Provider=SQLOLEDB;Data Source=<name of your SQL
    Server;Initial Catalog=pubs;User Id=<user name>"
       set rs = Server.CreateObject("ADODB.Recordset")
       rs.Open "Select * from Authors",cn
      
       'Instantiate an ADO Stream object to persist the recordset in 
       'XML format.
    
       Dim objStream
       Set objStream = Server.CreateObject("ADODB.Stream")
       objStream.Mode = 3   ' Read/Write mode
       objStream.Open     
    
       'Persist the recordset in XML format to the ADO stream object.
       rs.Save objStream, adPersistXML
    
       'Reposition stream pointer to the beginning of the stream, and 
       'return its contents to the client using the Write method
       'of the ASP Response object.
    
       objStream.Position = 0 
       Response.Write objStream.ReadText
       rs.Close
       Set rs = Nothing
       %>
    					
    Note The above code includes the Adovbs.inc file in the ASP. This facilitates the usage of ADO constant names instead of constant values in the ASP script. The Include statement specifies a relative reference. You will need to copy the Adovbs.inc file from the c:\Program Files\Common Files\System\ADO folder to the IIS virtual directory that contains the ASP.

  3. Save the file as datapage.asp in a virtual folder of your choice on your Web server.
  4. Open a new file in Notepad. Create an HTML page and embed the RDS data control in the page by specifying the appropriate OBJECT tag. The URL parameter of the RDS data control is set to datapage.asp, binding it to the recordset persisted by it.

    Copy and paste the following code into Notepad:
    <HTML>
    <BODY>
    
    <OBJECT CLASSID=clsid:BD96C556-65A3-11D0-983A-00C04FC29E33 id=dcPubs height=1 width=1>
    <PARAM NAME="URL" value="datapage.asp">
    </OBJECT>
    
    <INPUT id="txtfname" datasrc="#dcPubs" datafld="au_fname"> 
    <INPUT id="txtlname" datasrc="#dcPubs" datafld="au_lname">
    
    </BODY>
    </HTML>
    						
    The OBJECT tag in the HTML code is used to create an instance of the RDS data control on the HTML page. The URL parameter of the data control is set to datapage.asp.

    The two INPUT tags represent two instances of the HTML intrinsic text box control. The DATASRC and DATAFLD parameters of the INPUT tags are set to bind the text boxes to the au_fname and the au_lname fields in the ADO recordset persisted to the ASP Response Stream by datapage.htm.
  5. Save the HTML file as datapage.htm in the same virtual folder on your Web server where you saved datapage.asp in step 3.
  6. Launch Internet Explorer and type in the URL to access datapage.htm. The HTML page is displayed with two text boxes displaying the values of the au_fname and the au_lname columns in the first record of the ADO recordset persisted to the ASP Response stream by datapage.asp.
You can modify the preceding HTML code to bind the RDS data control to an HTML table and display all the records contained in the recordset.

The code sample in this article only works with MDAC versions 2.5 and later. The URL parameter of the RDS data control is a property introduced in ADO 2.5.

REFERENCES

For addition information, query on "Streams and Persistence" in the MSDN Library.

Modification Type:MinorLast Reviewed:7/15/2004
Keywords:kbhowto kbMSXMLnosweep KB259533