Tuesday, February 23, 2010

Complete ASP coding for PHONE BOOK

A small and complete project on Classic ASP. It is a PHONE BOOK PROGRAM. you can add new record, view all the records, edit a record or delete a record.

If you are able to understand this program, then you can easily do any other advanced asp programs.

Step 1: Check whether the folder"c:/inetpub/wwwroot" is available in your pc. If it is not there, then IIS which is the web server is NOT installed in your PC. For installing IIS, the procedure is given HERE . Once IIS installed, the above folder will be AUTOMATICALL CREATED..

Step 2. Create a new folder "PHONEBOOK" under c:/inetpub/wwwroot

Step 3: Create PhoneBook.mdb which is an MS access database under the folder PHONEBOOK

Step 4: Then, in the MDB, Create a Table with the name : PhoneBook In That Table, Create the fields Name (text 50), PhoneNo(50)

Step 5. Create the following asp files under the phonebook folder.(Note: This program has drawbacks (such as, no login page, no confirmation of deletion, no record id). It has been intentionally simplified. If you are able to understand this code, then you know 96% of ASP PROGRAMMING. For the remaining 4%, you purchase any book on ASP and read it.)

Copy this code in a notepad and save it as "c:/inetpub/wwwroot/phonebook/OpenConn.asp"


<!--CODE STARTS HERE-->

<%

Set Conn=Server.CreateObject("ADODB.Connection")

Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\inetpub\wwwroot\PhoneBook\PhoneBook.mdb;"

%>

<!--CODE ENDS HERE-->

====================================================

Copy this code in a notepad and save it as "c:/inetpub/wwwroot/phonebook/Default.asp"

<!--CODE STARTS HERE-->

<center>

PHONE BOOK

</center>

<br><br><br>

<table align=center cellpadding=5>

<tr> <td><a href="View.asp">View</a></td> </tr>

<tr> <td><a href="Add.asp">Add</a></td> </tr>

<tr> <td><a href="Edit.asp">Edit</a></td> </tr>

<tr> <td><a href="Delete.asp">Delete</a></td> </tr>

</table>


<%
Response.Write(Request("Feedback"))
%>

<!--CODE ENDS HERE-->

====================================================

Copy this code in a notepad and save it as "c:/inetpub/wwwroot/phonebook/Add.asp"

<!--CODE STARTS HERE-->

<html>

<head>

<title>Add New Record </title>

</head>

<body>

<P align="center"><STRONG>New Data Entry</STRONG></P>

<P align="center">

<form action="Add1.asp" Method="Post">

<TABLE border="1" >

<TR>

<TD>Name</TD>

<TD><input type="text" name="Name" size="40"></TD>

</TR>

<TR>

<TD>Phone Number</TD>

<TD><input type="text" name="PhoneNo" size="40"></TD>

</TR>

<TR>

<TD></TD>

<TD align="right"><input type="submit" value="Submit"></TD>

</TR>

</TABLE>

</form>

</P>

</body>

</html>

 

<!--CODE ENDS HERE-->

 

====================================================

Copy this code in a notepad and save it as "c:/inetpub/wwwroot/phonebook/Add1.asp"

<!--CODE STARTS HERE-->

<!--#include file="OpenConn.asp"-->
<%
Set Rs=Server.CreateObject ("ADODB.Recordset")
Rs.open "PhoneBook",Conn, 2 ,3
Rs.AddNew
Rs("Name")=Request.Form("Name")
Rs("PhoneNo")=Request.Form("PhoneNo")
Rs.Update
Response.Redirect("Default.asp?Feedback=Data Inserted Successfully")
%>

<!--CODE ENDS HERE-->

 

====================================================

 

Copy this code in a notepad and save it as "c:/inetpub/wwwroot/phonebook/Edit.asp"

<!--CODE STARTS HERE-->

<!--#include file="OpenConn.asp"-->
<html>
<head>
<title>Edit</title>
<%
sql="select * from PhoneBook order by Name"
Set Rs=Server.CreateObject("ADODB.Recordset")
Rs.Open Sql,Conn
%>
</head>
<body>
<%
If Rs.Eof Then
Response.write("No Records present for Editing")
Else
Response.write("Click on the Name to be edited")
While Not Rs.Eof
%>
<br> <a href="Edit1.asp?Name=<%=Rs("Name")%>"> <%=Rs("Name")%> </a>
<%
Rs.MoveNext
Wend
End If
%>

</body>
</html>

<!--CODE ENDS HERE-->

====================================================

Copy this code in a notepad and save it as "c:/inetpub/wwwroot/phonebook/Edit1.asp"

<!--CODE STARTS HERE-->

<!--#include file="OpenConn.asp"-->
<%
sql="Select * From PhoneBook where Name='" & Request("Name") &"'"
Set Rs=Conn.Execute(sql)
%>
<html>
<head>
<title>Edit </title>
</head>
<body>
<P align="center"><STRONG>Edit</STRONG></P>
<form action="Edit2.asp" Method="Post">
<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 Number</TD>
<TD><input type="text" name="PhoneNo" size="40" value="<%=Rs("PhoneNo")%>"></TD>
</TR>
<TR>
<TD></TD>
<TD align="right"><input type="submit" value="Update"></TD>
</TR>
</TABLE>
</form>
</P>
</body>
</html>

<!--CODE ENDS HERE-->

 

====================================================

Copy this code in a notepad and save it as "c:/inetpub/wwwroot/phonebook/Edit2.asp"

<!--CODE STARTS HERE-->

<!--#include file="OpenConn.asp"-->
<%
Set Rs=Server.CreateObject ("ADODB.Recordset")
Sql="select * from PhoneBook where Name='" & Request("Name") &"'"
Rs.Open Sql,Conn,2,3
Rs("PhoneNo")=Request.Form("PhoneNo")
Rs.Update
Response.Redirect("Default.asp?Feedback=Record Updated Successfully")
%>

<!--CODE ENDS HERE-->

====================================================

 

Copy this code in a notepad and save it as "c:/inetpub/wwwroot/phonebook/Delete.asp"

<!--CODE STARTS HERE-->

 

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

<html>
<head>
<title>Edit </title>
<%
sql="select * from PhoneBook order by Name"
Set Rs=Server.CreateObject("ADODB.Recordset")
Rs.Open Sql,Conn
%>
</head>
<body>
<%
If Rs.Eof Then
Response.write("No Records present for Deleting")
Else
Response.write("Click on the Name to be Deleted")
While Not Rs.Eof
%>
<br> <a href="Delete1.asp?Name=<%=Rs("Name")%>"> <%=Rs("Name")%> </a>
<%
Rs.MoveNext
Wend
End If
%>

</body>
</html>

<!--CODE ENDS HERE-->

 

====================================================

Copy this code in a notepad and save it as "c:/inetpub/wwwroot/phonebook/Delete1.asp"

<!--CODE STARTS HERE-->

<!--#include file="OpenConn.asp"-->
<%
Conn.Execute("delete from PhoneBook where Name='" & Request("Name") & "'")
Response.Redirect("Default.asp?Feedback=Record Deleted Successfully")
%>

 

<!--CODE ENDS HERE-->

====================================================

Copy this code in a notepad and save it as "c:/inetpub/wwwroot/phonebook/View.asp"

<!--CODE STARTS HERE-->


<!--#include file="OpenConn.asp"-->
<html>
<head>
<title>View</title>
<%
sql="select * from PhoneBook order by Name"
Set Rs=Server.CreateObject("ADODB.Recordset")
Rs.Open Sql,Conn
%>
</head>
<body>
<%
If Rs.Eof Then
Response.write("No Records present .")
Else

While Not Rs.Eof
%>
<br> <%=Rs("Name")%> *** <%=Rs("PhoneNo")%>
<%
Rs.MoveNext
Wend
End If
%>

</body>
</html>
<!--CODE ENDS HERE-->

====================================================

Last step : after creating all the above asp files inside the folder "c:/inetpub/wwwroot/phonebook" , open the brower and type

"localhost/phonebook/default.asp" . You will get the page as shown below.