Microsoft KB Archive/259533

= How To Use an ASP Page as a Data Source for an RDS Data Control =

Article ID: 259533

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7
 * Microsoft Active Server Pages 4.0

-



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.



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:  Open Notepad.  Copy and paste the following code into a new file:

Note You must change User Id= 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. <%  Dim cn   Dim rs   Set cn = Server.CreateObject(&quot;ADODB.Connection&quot;) cn.Open &quot;Provider=SQLOLEDB;Data Source=<name of your SQL Server;Initial Catalog=pubs;User Id= &quot; set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;) rs.Open &quot;Select * from Authors&quot;,cn 'Instantiate an ADO Stream object to persist the recordset in   'XML format.

Dim objStream Set objStream = Server.CreateObject(&quot;ADODB.Stream&quot;) 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.

 Save the file as datapage.asp in a virtual folder of your choice on your Web server.  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:  

  

<INPUT id=&quot;txtfname&quot; datasrc=&quot;#dcPubs&quot; datafld=&quot;au_fname&quot;> <INPUT id=&quot;txtlname&quot; datasrc=&quot;#dcPubs&quot; datafld=&quot;au_lname&quot;>

</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. </li> Save the HTML file as datapage.htm in the same virtual folder on your Web server where you saved datapage.asp in step 3.</li> 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.</li></ol>

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.

<div class="references_section">