473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ


Connect to Databases

By Robert Murdock
Programmer, Darpac Inc.

Connecting to a database:

For our example we will connect directly to the database in the working server directory. In real world apps you may want to make a data directory and create a DSN at the NT level. Please note there are several ways to do any one task in ASP. I will show you a very simple example and you may experiment once you understand how it works.

Here is the code that you can use to gather info and insert it into your database:

File: new.asp

<html>
 <head>
 <title>Create Entry</title>
 </head>
   
 <body BGCOLOR="#FFFFFF">
   ' This include is used for ADO access w/VB Script
 <!--#include file="adovbs.inc"-->
 <%
   'Check to see if this is the first time the page has been loaded.
   'If there is data to be processed, this section will execute.
   If Request.ServerVariables("Content_Length")>0 Then
   If "Insert" = Request("ActionType") Then
      ' Create a connection to the database.
      Set DataConn = Server.CreateObject("ADODB.Connection")
      Set RS = Server.CreateObject("ADODB.RecordSet")
      DataConn.Open "DBQ=" & Server.Mappath("cdemo.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"
   
      'Open the record table and then insert the record.  
          RS.Open "tblContact", DataConn, adOpenKeyset, adLockOptimistic 
      RS.AddNew
      
      RS("Name") = Request("Name")
      RS("Email") = Request("Email")
      RS("Phone") = Request("Phone")
      RS.Update
     
      'Cleanup - Close database connection.
      RS.Close
      DataConn.Close
      
      ' Send text to the user to know they have completed the 
      ' process with no errors. Sever will display errors fatal errors.
      Response.Write "<CENTER>Thank you!</CENTER>"
   Else
   %>
   ' Show the user what they are about to save.
 <pre>
   Name:  <b><%=Request("Name")%></b>
   Email: <b><%=Request("Email")%></b>
   Phone: <b><%=Request("Phone")%></b>
 </pre>
   
 <p><br>
   Do you want to submit this report as shown (Hit "back" on your browser to edit)?
 </p>
   
 <form METHOD="POST" ACTION="<%=Request.ServerVariables("SCRIPT_NAME")%>">
   <input type="hidden" name="Name" value="<%=Request("Name")%>"><input type="hidden"
   name="Email" value="<%=Request("Email")%>"><input type="hidden" name="Phone"
   value="<%=Request("Phone")%>"><input type="hidden" name="ActionType" value="Insert"><p><input
   TYPE="SUBMIT" VALUE="Yes!"> </p>
 </form>
 <%  
   End If
   Else
   %>
   'Input new record information. 
 <form ACTION="<%=Request.ServerVariables("SCRIPT_NAME")%>" METHOD="POST">
   <div align="left"><p>Welcome. Please fill in all fields.<br>
   </p>
   </div><blockquote>
     <div align="center"><div align="center"><center><table border="0" width="512" height="78"     cellspacing="0" cellpadding="0">
       <tr>
         <td width="500" height="22" colspan="2">Enter Information.<br></td>
       </tr>
       <tr>
         <td width="171" height="7">Name</td>
         <td width="329" height="7"><input type="text" name="Name" size="35"></td>
       </tr>
       <tr>
         <td width="171" height="7">Email</td>
         <td width="329" height="7"><input type="text" name="Email" size="35"></td>
       </tr>
       <tr>
         <td width="171" height="7">Phone</td>
         <td width="329" height="7"><input type="text" name="Phone" cols="35"></td>
       </tr>
     </table>
     </center></div><div align="center"><center><table>
       <tr>
         <td><div align="center"><center><p><input TYPE="SUBMIT" VALUE="Preview"></td>
       </tr>
     </table>
     </center></div></div>
   </blockquote>
 </form>
 <% End If %>
 </body>
 </html>
   

As you can see this page will display the fields the user will need to enter, display the results as a preview page then add the info to the database and display a thank you. I find this method better then making multiple pages to handle one task.

Now a simple update page for this we will show the input section as an html doc that passes the data to the update script that will update the information in the database. You could use the same method of posting back to your .asp file as in the first example. I broke this up so you would be able to see both methods.

File: update.htm

<html>
 <head>
 <title>Update Information</title>
 </head>
   
 <body BGCOLOR="#ffffff">
   
 <form ACTION="update.asp" METHOD="POST">
   <div align="left"><p>Welcome. Please fill in all fields.<br>
   </p>
   </div><blockquote>
     <div align="center"><div align="center"><center><table border="1" width="512" height="78">
       <tr>
         <td width="500" height="22" colspan="2">Update Info:</td>
       </tr>
       <tr>
         <td width="83" height="7">Name</td>
         <td width="417" height="7"><input type="text" name="Name" size="20"></td>
       </tr>
       <tr>
         <td width="83" height="7">Email</td>
         <td width="417" height="7"><input type="text" name="Email" size="20"></td>
       </tr>
       <tr>
         <td width="83" height="7">Phone</td>
         <td width="417" height="7"><input type="text" name="Phone" size="20"></td>
       </tr>
     </table>
     </center></div><div align="center"><center><table>
       <tr>
         <td><div align="center"><center><p><input TYPE="SUBMIT" VALUE="Update"></td>
       </tr>
     </table>
     </center></div></div>
   </blockquote>
 </form>
 </body>
 </html>
   

« ASP Databases Updating Records »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.