Web Developer 2

ActiveX Data Objects

Example: The Guest Book (Get the code here)

Viewing Records

Here is the default.asp file that displays options for the user:

<html>

<head>

<title>Guest Book</title>

</head>

<body>

<h2>Guest Book - Options</h2>

<p><a href="guestbook.asp">View the entire Guest Book</a></p>

<form method="get" action="search.asp">

<p>Search the Guest Book for: <input type="text" name="keyword"><input type="submit" value="Search"></p>

</form>

<p><a href="sign.asp">Sign the Guest Book</a></p>

</body>

</html>

 

Here is the guestbook.asp file that displays all of the guests:

<%@ language="JavaScript" %>

<!-- #include file="connection.asp" -->

<%

var rs;

rs = Server.CreateObject("ADODB.RecordSet");

rs.Open("select * from guestbook order by date desc", con);

%>

<html>

<head>

<title>All Entries</title>

</head>

<body>

<h2>Guest Book</h2>

<table border="0" cellpadding="6">

<%

while(!rs.EOF){

%>

<tr>

<td bgcolor="#FFFFCC"><%= rs("name") %></td>

<td bgcolor="#FFFFCC" rowspan="3"><%= rs("comments") %></td>

</tr>

<tr>

<td bgcolor="#FFFFCC"><%= rs("eMail") %></td>

</tr>

<tr>

<td bgcolor="#FFFFCC"><%= rs("date") %></td>

</tr>

<%

rs.MoveNext();

}

%>

</table>

<p><a href="default.asp">Guest Book Home</a></p>

</body>

</html>

The above uses the connection.asp file to connect to the database.  It’s always a good idea to use an include file to hold your connection information.  That way, should your database change, you’ll only need to modify your application in one place.  Here’s the connection.asp file:

<%

var con;

con = Server.CreateObject("ADODB.Connection");

con.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath("\\WebDev2\\duffy\\guestbook\\db\\guestbook.mdb"));

%>

Note that your connection string will be different.  If you are using Brinkster, and you’ve stored your .mdb file in the db folder, your connection string will end with:

Server.MapPath(\\yourUserName\\db\\guestbook.mdb)

Adding Records

Here is the code to sign the guestbook.  Note that there is an HTML form (sign.asp) and a form handler (submit.asp).  After writing to the database, the form handler redirects to the guestbook.asp file.  Note that the date field is generated – you wouldn’t want to let users create the date.  Here is sign.asp:

<html>

<head>

<title>Please Sign In!</title>

</head>

<body>

<h2>Please Sign In!</h2>

<form method="post" action="submit.asp">

<p>Name: <input type="text" name="name"></p>

<p>E-Mail: <input type="text" name="eMail"></p>

<p>Comments:<br><textarea rows="6" cols="26" name="comments"></textarea></p>

<p><input type="submit" value="Sign In"><input type="reset" value="Start Over"></p>

</form>

<p><a href="javascript:history.go(-1)">Cancel</a></p>

</body>

</html>

And here is submit.asp:

<%@ language="JavaScript" %>

<!-- #include file="connection.asp" -->

<%

var cmd;

cmd = Server.CreateObject("ADODB.Command");

cmd.ActiveConnection = con;

var now = new Date();

var month = now.getMonth();

var date = now.getDate();

var year = now.getYear();

var timestamp = year + '/' + (month + 1) + '/' + date;

var guestCommand = "insert into guestbook values('";

guestCommand += Request("name") + "', '";

guestCommand += Request("eMail") + "', '";

guestCommand += Request("comments") + "', '";

guestCommand += timestamp + "')";

cmd.CommandText = guestCommand;

Response.Write(guestCommand);

//cmd.Execute();

con.close();

//Response.Redirect("guestbook.asp");

%>

Searching for Records

Here is the searchResults.asp file.  This file reads the search keyword, builds a SQL statement, then queries the database using a “like” clause.  It then displays any matching records using the display.asp include file:

<%@ language="JavaScript" %>

 

<!-- #include file="connection.asp" -->

<%

var rs;

rs = Server.CreateObject("ADODB.RecordSet");

var keyword = Request("keyword");

var query = "select * from guestbook where name like '%";

query += keyword;

query += "%' or eMail like '%";

query += keyword;

query += "%' or comments like '%";

query += keyword;

query += "%' order by date desc";

rs.Open(query, con);

%>

<html>

<head>

<title>Search Results</title>

</head>

<body>

<h2>Guest Book Search Results</h2>

<!-- #include file="display.asp" -->

<p><a href="default.asp">Guest Book Home</a></p>

</body>

</html>

Here is the display.asp file:

<table border="0" cellpadding="6">

<%

while(!rs.EOF){

%>

<tr>

<td bgcolor="#FFFFCC"><%= rs("name") %></td>

<td bgcolor="#FFFFCC" rowspan="3"><%= rs("comments") %></td>

</tr>

<tr>

<td bgcolor="#FFFFCC"><%= rs("eMail") %></td>

</tr>

<tr>

<td bgcolor="#FFFFCC"><%= rs("date") %></td>

</tr>

<%

rs.MoveNext();

}

%>

</table>

Note that display.asp can ONLY be used as an include file as it doesn’t possess any of the HTML code necessary for a complete document.  Also note that the RecordSet object MUST be named “rs” – it will fail if that isn’t the case.