Phone Book - Complete ASP Coding

Create a folder "phonebook" under c:/inetpub/wwwroot and save the following files .

Then open the browser and type : http://localhost/phonebook/welcome.asp

Create the following MS Access Database
Name of the mdb : phonebook.mdb
Location : c:\inetpub\wwwroot\phonebook
Table Name : phonebook_table
Fields: name ..........Text(50)
phoneno.................Text(35)
record_id...............Auto Number

<!--Save this file as menu_inc.asp-->

<table align="center" cellpadding="5">
<tr><td align="center">
<img src="monkey001.gif" border="0" />
</td>
</tr>
</table>
<table align="center" cellpadding="5">
<tr>
<td><a href="http://aspspider.ws/lamens">
Home
</a>
</td>
<td><a href="view.asp">View</a></td>
<td><a href="add.asp">Add</a></td>
<td><a href="edit.asp">Edit</a></td>
<td><a href="delete.asp">Delete</a></td>
</tr>
</table>

!-- save this file as welcome.asp -->

<html>
<head>
<title>Welcome</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<!--#include file="menu_inc.asp"-->
<table align="center">
<tr>
<td align="center" style="font:bold 30px arial;color:#ff0000;">
Welcome to My Phone Book
</td>
</tr>
</table>
</body>
</html>
At this stage, you can test your program by opening the browser and typing: http://localhost/phonebook/welcome.asp
<!--#include file="open_conn_inc.asp"-->
<!--Save this file as view.asp -->
<html>
<head>
<title>View</title>
<%
sql="select * from phonebook_table order by Name"
Set Rs=Server.CreateObject("ADODB.Recordset")
Rs.Open Sql,Conn
%>
</head>
<body>
<!--#include file="menu_inc.asp"-->
<%
'whenever a record is added or edited, this page will be opened
'with a request variable : postback_msg. We have to display the
'value of this postback_msg variable.
If Request("postback_msg") <>"" Then
'if this variable is not empty
%>
<font color="red" size="4"><strong>
<%=Request("postback_msg")%>
</strong></font>
<%
End If
%>
<%
If Rs.Eof Then
Response.write("No Records present for Editing")
Else
%>
<table border="1" align="center">
<tr>
<th>Name</th>
<th>Phone No.</th>
</tr>
<%
While Not Rs.Eof
%>
<tr>
<td>
<%=Rs("name")%>
</td>
<td>
<%=Rs("phoneno")%>
</td>
</tr>
<%
Rs.MoveNext
Wend
%>
</table>
<%
End If
%>

</body>
</html>

<%
' Save this file as : open_conn_inc.asp
Set Conn=Server.CreateObject("ADODB.Connection")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("phonebook.mdb") & ";"
'for aspspider web hosting, the following line is used.
'Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("..\..\database\phonebook.mdb") & ";"
%>
<%
' Save this file as adovbs.inc
' this is NOT a complete adovbs.inc file.
' lines NOT related to our project have been deleted.
'---- CursorTypeEnum Values ----
Const adOpenForwardOnly = 0
Const adOpenKeyset = 1
Const adOpenDynamic = 2
Const adOpenStatic = 3

'---- LockTypeEnum Values ----
Const adLockReadOnly = 1
Const adLockPessimistic = 2
Const adLockOptimistic = 3
Const adLockBatchOptimistic = 4

'---- CursorLocationEnum Values ----
Const adUseServer = 2
Const adUseClient = 3

%>

<!--Save this file as : add.asp -->
<html>
<head>
<title>Add</title>
</head>
<body>
<!--#include file="menu_inc.asp"-->
<form action="add1.asp" Method="Post">
<table border="1" align="center">
<tr>
<td> Name </td>
<td><input type="text" name="name" size="40">
</td>
</tr>
<tr>
<td> Phone No. </td>
<td><input type="text" name="phoneno" size="12">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>

<!--Save this file as add1.asp-->
<!--#include file="open_conn_inc.asp"-->
<!--#include file="adovbs.inc"-->
<%
Set Rs=Server.CreateObject ("ADODB.Recordset")
sql="select * from phonebook_table"
'Rs.CursorLocation=3
Rs.open sql,Conn, adOpenDynamic,adLockOptimistic
'If Rs.RecordCount > 20 Then
' Conn.Execute("delete from phonebook_table where record_id=(select min(record_rd) from phonebook_table)")
'End If
' The above 4 commented lines are not required for you. forget them.
Rs.AddNew
Rs("Name")=Request.Form("Name")
Rs("PhoneNo")=Request.Form("PhoneNo")
Rs.Update
Response.Redirect("view.asp?postback_msg=Data Inserted Successfully")
%>
At this stage, you can test your program for adding a record.
<!--Save this file as edit.asp -->
<!--#include file="open_conn_inc.asp"-->
<%
sql="select * from phonebook_table"
Set Rs=Server.CreateObject("ADODB.Recordset")
Rs.Open Sql,Conn
%>

<html>
<head>
<title>Edit</title>
</head>
<body>
<!--#include file="menu_inc.asp"-->
<%
If Rs.Eof Then
Response.write("No Records present for Editing")
Else
%>
<table border="1" align="center">
<tr>
<th colspan="2" align="center">
Select any record to Edit
</th>
</tr>
<tr>
<th>Name</th>
<th>Phone No.</th>
</tr>
<%
While Not Rs.Eof
%>
<tr>
<td>
<a href="edit1.asp?record_id=<%=Rs("record_id")%>">
<%=Rs("name")%>
</a>
</td>
<td>
<%=Rs("phoneno")%>
</td>
</tr>
<%
Rs.MoveNext
Wend
%>
</table>
<%
End If
%>

</body>
</html>

<!--Save this file as edit1.asp -->
<!--#include file="open_conn_inc.asp"-->
<%
sql="select * from phonebook_table where record_id=" & Request("record_id")
Set Rs=Conn.execute(sql)
%>
<html>
<head>
<title>Edit</title>
</head>
<body>
<!--#include file="menu_inc.asp"-->
<form action="edit2.asp" Method="Post">
<input type="hidden" name="record_id" value="<%=Request("record_id")%>">
<table border="1" align="center">
<tr>
<td> Name </td>
<td><input type="text" name="name" size="40" value="<%=Rs("name")%>">
</td>
</tr>
<tr>
<td> Phone No. </td>
<td>
<input type="text" name="phoneno" size="12" value="<%=Rs("phoneno")%>">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Update">
</td>
</tr>
</table>
</form>
</body>
</html>

<!-- Save this file as edit2.asp -->
<!--#include file="open_conn_inc.asp"-->
<%
conn.Execute("delete from phonebook_table where record_id=" & Record_id)
Response.Redirect("view.asp?postback_msg=Record DELETED Successfully")
%>
Final Note: To reduce the number of coding lines to the barest minimum, the above program is NOT written in a perfect manner. For example certain things like form validation, closing of opened database connection,etc have been avoided.
Thank you spending your time here.
If possible you; can leave some comments or suggestions.
Thanks a lot once again.
Google Search
Disclaimer and Copy Right