Helping Canadian Canadian Flag  
business thrive on the web.
 

Home Hosting Plans-shared Hosting-Ecommerce Dedicated Servers Domain Registration Web Design Internet Access Resellers Terms & AUP Consulting Support

 
info@chinookwebs.com Web Hosting phone number for Calgary 403.257.1357    
 

Scripting- Newsletter Subscription by Tim McKay

 
 

Newsletter Signup Script
ASP using ADO Example

Author Timothy McKay, AnyKey Solutions Ltd.

Writing ASP to access databases over the Internet is surprisingly easy. This article describes how a programmer with novice to intermediate experience could write a simple "add email to newsletter" script.

Let's start with the basics of Internet Database Development. What is ADO?

ADO stands for ActiveX Data Objects. ADO allows the savvy Web Programmer the ability to access ODBC DataSources on the Web Server. Both ODBC and ADO are Microsoft products, so naturally these capabilities are available only with Microsoft's Internet Information Services (IIS).

The Situation

This example employs an HTML form and the use of Microsoft Access databases. This article is written for Web Developers with a good handle on these technologies. If I may be of assistance with these areas, please feel free to write me at tm@anykey.ca

If you would like to play along, there's a small file, below, which you can download. The file includes everything you need to get going (except, of course an IIS server with DSN configuration access). Ok, let's get this ball rolling...

When the file Newsletter.Htm is displayed an HTML form is presented with a single entry field where the user types their email address. Once the user clicks on the Subscribe/Unsubscribe button, the form posts the data to the Signup.Asp file. That's where the magic happens. I made some comments through the code where I though ADO points should be made. Database

I created a Microsoft Access 97 MDB file when I tested this example. It contains one table, "Addresses". The Addresses table contains one field, "Email", which is a Text field with a width of 50 characters.

Script Code There are two files, the Newsletter.Htm and Signup.Asp. Both are listed on this page for you to use. All I ask is that you keep the copyright info in the file, thanks.

For added convenience, you can download a file which contains the source you need to get this going.

Newsletter.Htm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- Copyright (c) 2002 AnyKey Solutions Ltd. All rights reserved. -->
<html>
<head>
    <meta http-equiv="author" content="Timothy McKay">
    <title>Newsletter Signup ASP using ADO Example</title>
</head>

<style>
<!-- .normal {
    font-family: Arial, Verdana, Geneva, Helvetica, sans-serif;
    font-size: 12px;
    text-decoration : none;
    color: #000066;
} .error {
    font-family: Arial, Verdana, Geneva, Helvetica, sans-serif;
    font-size: 12px;
    text-decoration : none;
    color: #dd0000;
} .header {
    font-family: Arial, Verdana, Geneva, Helvetica, sans-serif;
    font-size: 24px;
    text-decoration : bold;
    color: #000066;
}
-->
</style>

<body text="#c0c0c0" bgcolor="#0080ff">
    <p class=header>Newsletter Signup

    <p class=normal>To signup for our newsletter, enter your name
    below and click    the button.<br><br>

    If your email address already exists, you will be removed from
    the distribution list.

    <form action="signup.asp" method="POST">
        <b>Your Email Address:</b>
        <input type="text" size="35" name="emailaddress"><br><br>
        <input type="submit" name="submit"
            value="Subscribe/Unsubscribe">
    </form>

    <p class=normal>
    <b>NOTE : </b><br><br>

    <i>Your email address is a sacred thing. We will not abuse
    your email address by giving it to anybody for any reason.</i>
</body>
</html>

Signup.Asp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- Copyright (c) 2002 AnyKey Solutions Ltd. All rights reserved. -->
<html>
<head>
    <meta http-equiv="author" content="Timothy McKay">
    <title>Newsletter Signup ASP using ADO Example</title>
</head>

<style>
<!-- .normal {
    font-family: Arial, Verdana, Geneva, Helvetica, sans-serif;
    font-size: 12px;
    text-decoration : none;
    color: #000066;
} .error {
    font-family: Arial, Verdana, Geneva, Helvetica, sans-serif;
    font-size: 12px;
    text-decoration : none;
    color: #dd0000;
} .header {
    font-family: Arial, Verdana, Geneva, Helvetica, sans-serif;
    font-size: 24px;
    text-decoration : bold;
    color: #000066;
}
-->
</style>

<body text="#c0c0c0">
<%
    DIM Conn, Rs
    Set Conn = CreateObject("ADODB.Connection")
    Set Rs = CreateObject("ADODB.Recordset")

Up to this point, there haven't been any big surprises. These statements might need a little explaining though.

As you can see, I defined two variables, Conn and Rs. These variables will become references to two objects; the ADDODB.Connnection and ADODB.Recordset objects, specifically.

The ADODB.Connection object is the heartbeat that you have to the ODBC DataSource on the server. The connection handles the authentication to the database, caching, and all sorts of nifty things we don't need to worry about today.

The ADODB.Recordset object is where things liven up a bit. The Recordset object supports some basic data handling methods and properties, including :
  • Open Method Can be used to open a table, or to execute SQL statements of your choosing.
  • Close Method This will do pretty much that. One trait of non-row-returning SQL statements, like INSERT, DELETE, or UPDATE is that they do not return a result set, and therefore you do not call the Close Method, or you will get an error.
  • MoveFirst Method Moves the record pointer to the first record in the record set.
  • MoveLast Method Moves the record pointer to the last record in the record set.
  • MoveNext Method Moves the record pointer to the next record in the record set.
  • MovePrevious Method Moves the record pointer to the record immediately prior to the current one in the record set.
  • BOF Property Boolean. Lets you know if the current record pointer is at the Beginning of record set.
  • EOF Property Boolean. Lets you know if the current record pointer is at the End of the record set.

    Conn.Open "DataSourceName"

This is an invocation of the ADODB.Connection Open method. The "DataSourceName" must exist on the server as a valid System DSN pointing to the database that the script expects or the script will fail.

If you want to try this at home, you can download the entire scripts along with a sample database, below.

    DIM sEmailAddress

    sEmailAddress = Request.Form("emailaddress")

    If Len(sEmailAddress)=0 Then
        Response.Write "<p class=header>Signup Error!"
        Response.Write "<p class=error><B>No email " & _
            "address was supplied!</B><BR><BR>"
        Response.Write "Go back, and try again..."
        Response.Write "<a class=link href=newsletter.htm>" & _
            " (Click Here)</a><BR><BR>"
    Else    
        sEmailAddress = LCase(Trim(sEmailAddress))

        Rs.Open "SELECT * FROM Addresses WHERE Email = '" & _
                sEmailAddress & "'", Conn

        If Rs.Eof then

As you can see, we're starting to use the ADODB.Recordset object now (Rs.Open & Rs.Eof). I'm opening a row-returning SQL SELECT statement. This is a test to see whether the email address exists in the system or not. If it does not exist, we will add it; otherwise, we will remove it.

            Response.Write "<p class=header>"
            Response.Write "Adding To Newsletter"
        
            Rs.Close
            Rs.Open "INSERT INTO Addresses (Email) " & _
                 " VALUES ('" & sEmailAddress & _
                 "')", Conn
            
            Response.Write "<p class=normal>"
            Response.Write "<b>" & sEmailAddress & "</B>"
            Response.Write " was successfully added!<BR><BR>"    
        Else
            Response.Write "<p class=header>"
            Response.Write "Removing From Newsletter"
        
            Rs.Close
            Rs.Open "DELETE FROM Addresses " & _
                    " WHERE Email='" & _
                    sEmailAddress & "'", Conn
            
            Response.Write "<p class=normal>"
            Response.Write "<b>" & sEmailAddress & "</B>"
            Response.Write " was successfully removed!<BR><BR>"
        End If
    End If

    Conn.Close
This is probably the most important statement in this entire script.

Most servers are configured to handle a maximum number of open data connections per user session. If you open (but never close) data connections then your visitor is only a few page impressions away from a lovely server crash.

That won't (or shouldn't) bring the server crashing down, but what it will do is deadlock any further attempts to access that ODBC DataSource. Indeed, the DataSource is crashed due to too many open connections. Data connections can be forced shut, from the server-end. An IIS administrator would need to Unload the scripts for that Virtual Directory or Folder. Check with your Domain Host's technical support if you need to have the data connections forced shut.

%>
</body>
</html>


Downloads

If you would like to try this example, but don't want to cut & past the code, then download the file FormToADOSample.zip, which is a WinZip Archive (ZIP file), containing:

  • Newsletter.htm This HTML file has a form which launches the ASP to post to the database.
  • Signup.Asp This ASP file has the VBScript code which uses ADO to update records in an Access Database.
  • FormToADOSample.mdb This is an Access Database (created using Access 97) containing a single Table, "Addresses".

REMEMBER! Before this example will work, you will need to open the ODBC Administrator (should be in your Windows Control Panel or Administrator Options within the Control Panel) and create the following Data Source Name:

Data Source Name = DataSourceName
Path = [?? pick a folder on your system ??] \ FormToADOSample.mdb


There you have it. One newsletter signup script in ASP that posts to an Access Database via ADO!

I'd love to hear your feedback. Feel free to write me Timothy McKay .

 
   
     
     
 
  :::: Privacy Policy
 
 
Home Overview Hosting-shared Hosting-Ecommerce Hosting-Semi Dedicated Domain Registration
Web Development Internet Access Resellers Terms & AUP Consulting About Us
FrontPage Support Chinook Store Scripting Technical Overview