Microsoft KB Archive/299637

= How To Process Data Transactions from an ASP Page =

Article ID: 299637

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q299637





IN THIS TASK
SUMMARY
 * Prerequisites
 * Processing Data Transactions from ASP

REFERENCES



SUMMARY
This step-by-step procedure demonstrates how to process data transactions from an Active Server Pages (ASP) page if the transactions use ActiveX Data Objects (ADO) to connect to a database.

back to the top

Prerequisites
This list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:


 * Microsoft Active Server Pages
 * Microsoft Internet Information Server

This article assumes that you are familiar with the following topics:


 * ASP terminology and syntax
 * Data Access technologies (Microsoft ActiveX Data Objects)

back to the top

Processing Data Transactions from ASP
  In Notepad, create a new ASP page named DataTran.asp, and paste the following code:

Note You must change UID= and pwd= to the correct values before you run this code. Make sure that UID has the appropriate permissions to perform this operation on the database.

<%@ TRANSACTION=Required%> <%   Option Explicit On Error Resume Next Dim oConn, oRS Set oConn = Server.CreateObject(&quot;ADODB.Connection&quot;) oConn.Open &quot;Provider=SQLOleDB;server=servername;Initial Catalog=pubs;uid= ;pwd= &quot; if err.Number <> 0 Then Response.Write &quot;Error Occurred Opening Connection...&quot; Response.Write &quot;Error Description: &quot; & err.Description & &quot;...&quot; ObjectContext.SetAbort Response.End else Response.Write &quot;Connection Opened Successfully...&quot; ObjectContext.SetComplete End If

oConn.Execute &quot;Select * from Authors&quot; if err.Number <> 0 Then Response.Write &quot;Error Occurred Executing Query...&quot; Response.Write &quot;Error Description: &quot; & err.Description & &quot;...&quot; oConn.Close Set oConn = Nothing ObjectContext.SetAbort Response.End else Response.Write &quot;Query Completed Successfully...<BR>&quot; ObjectContext.SetComplete End If

oConn.Close Response.Write &quot;<BR>Connection Closed Successfully...<BR>&quot; set oConn = Nothing Response.Write &quot;<BR>Test Completed Successfully...<BR>&quot;

Sub OnTransactionCommit Response.Write &quot; The Transaction just committed.&quot; Response.Write &quot;This message came from the &quot; Response.Write &quot;OnTransactionCommit event handler.&quot; End Sub

Sub OnTransactionAbort Response.Write &quot; The Transaction just aborted.&quot; Response.Write &quot;This message came from the &quot; Response.Write &quot;OnTransactionAbort event handler.&quot; End Sub

%>                   </li> <li> The above code sample tries to connect to the Microsoft SQL Server default PUBS database. If you do not have SQL Server installed, you must change the connection string and the SQL statement.

Also, make sure that you change the connection string in the following line of code to include the correct server name, User Id (UID) and Password (PWD): oConn.Open &quot;Provider=SQLOleDB;server=servername;Initial Catalog=pubs;uid= ;pwd= &quot; </li> <li>On the File menu, click Save. In the Save in drop-down list box, click the C:\Inetpub\Wwwroot folder. In the Save as type drop-down list box, click All Files. In the File name list box, type DataTran.asp .</li> <li>In your Web browser, run the ASP page (for example, http://localhost/DataTran.asp).</li> <li>If the OLE DB connection string and query are valid, the following output is returned:

<pre class="fixed_text">Connection Opened Successfully...

Query Completed Successfully...

Connection Closed Successfully...

Test Completed Successfully...

The Transaction just committed.This message came from the OnTransactionCommit event handler.

</li> <li>If the OLE DB connection string is invalid, or if your query is incorrect, you receive an error message, and the OnTransactionAbort event is fired. The following output is returned:

<pre class="fixed_text">Connection Opened Successfully...

Error Occurred Executing Query...

Error Description: Invalid object name 'Author'....

The Transaction just aborted.This message came from the OnTransactionAbort event handler.

</li></ol>

back to the top

<div class="references_section">