Practical-19 Aim: Create Webpage To Add, Update, Delete Records Form Database Using Objects of ADO Create Database

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

PRACTICAL-19

Aim: Create webpage to add,update,delete records form database using


objects of ADO

Create Database:
1) Create Database in Microsoft Office Access and give name “Students”
2) Save as this database as Microsoft Office Access 2002-2003 Format. So now its extension
is “.mdb”(Right Click on Database and check Type of File)
3) Create “student” table and create fields as shown in image

4) create webdata folder in C drive and put your database in webdata folder.( so the path will
be c:/webdata/Students.mdb)

1. Student_Entry.html
<html>
<head>
<title>Student Form</title>
</head>
<body>
<form method="post" action="add_Student.asp">
<br>
<P align=center><FONT size=5><U><STRONG>Student
Entry</STRONG></U></FONT></P>
<P align=center>
<A href="Student_Data.asp">VIEW
STUDENTS</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<A
href="Student_Entry.html">INSERT</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<A
href="Student_update_select.asp">UPDATE</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<A href="Student_delete_select.asp">DELETE</A>
</P>
<hr>
<table align=center border=0>
<tr>
<td><b>Enter Enrollment No:</td>
<td><input type="text" name="eno" maxlength="20"></td>
</tr>

Enrollment No: Batch: 4th Sem IT Page No:


<tr>
<td><b>Enter Your Name:</td>
<td><input type="text" name="name" maxlength="20"></></td>
</tr>
<tr>
<td><b>Enter College Name:</td>
<td><input type="text" name="clg" maxlength="20"></td>
</tr>
</table><Br>
<center> <input type ="submit" name="submit" value="submit"> </center>
</form>
</body>
</html>

2. Student_Data.asp
<Html>
<head>
<TITLE>Information Tech Dept</TITLE>
<LINK REL="STYLESHEET" HREF="table.css">
</head>

<Body>
<P align=center><FONT size=5><U><STRONG>Student
Record</STRONG></U></FONT></P>
<P align=center>
<A
href="Student_Entry.html">INSERT</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<A
href="Student_update_select.asp">UPDATE</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<A href="Student_delete_select.asp">DELETE</A>
</P>
<hr>
<%
Dim Conn,objrs,sql
Set Conn = Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open("c:/webdata/Students.mdb")

Set objrs=server.createobject("ADODB.Recordset")
sql="select * from student"
objrs.open sql,conn
%>
<center>
<br>
<font face = 'arial' color = 'blue'><b>Information Technology Department</b></font>
<br>
<TABLE border=1>
<TR>

Enrollment No: Batch: 4th Sem IT Page No:


<th align='center' bgcolor='#b0c4de'>Enrollment No</th>
<th align='center' bgcolor='#b0c4de'>Student Name</th>
<th align='center' bgcolor='#b0c4de'>College Name</th>

</TR>
<% do While not objrs.EOF %>
<%="<tr><td>"& objrs("Enrollment_no") %></td>
<%="<td>"& objrs("Student_Name") %></td>
<%="<td>"& objrs("College_Name") %></td>
<%
objrs.Movenext
Loop
objrs.Close
conn.Close
%>
</table>
</body>
</html>

3 add_Student.asp
<%
Dim adoCon
Dim rsAddstudent
Dim strSQL

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


adoCon.provider="Microsoft.Jet.OLEDB.4.0"
adoCon.open "c:\webdata\Students.mdb"

Set rsAddstudent=Server.CreateObject("ADODB.Recordset")
strSQL="SELECT student.Enrollment_No,
student.Student_Name,student.College_Name FROM student;"

rsAddstudent.CursorType=2
rsAddstudent.LockType=3
rsAddstudent.Open strSQL, adoCon

rsAddstudent.AddNew

rsAddstudent.Fields("Enrollment_No")=Request.Form("eno")
rsAddstudent.Fields("Student_Name")=Request.Form("name")
rsAddstudent.Fields("College_Name")=Request.Form("clg")

rsAddstudent.Update
rsAddstudent.Close
Set rsAddstudent = Nothing
Set adoCon = Nothing

Response.Redirect "Student_Data.asp"
%>

Enrollment No: Batch: 4th Sem IT Page No:


4. Student_upadate_select.asp
<html>
<head>
<title>Update Student Data</title>
</head>
<body bgcolor="white" text="black">
<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsstudent 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database

'Create an ADO connection odject


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

'Set an active connection to the Connection object using a DSN-less connection


adoCon.Provider="Microsoft.Jet.OLEDB.4.0"
adoCon.Open "c:/webdata/Students.mdb"

'Set an active connection to the Connection object using DSN connection


'adoCon.Open "DSN=guestbook"

'Create an ADO recordset object


Set rsstudent = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT student.* FROM student;"

'Open the recordset with the SQL query


rsstudent.Open strSQL, adoCon
%>
<P align=center><FONT size=5><U><STRONG>Select Enrollment No For Update Record
</STRONG></U></FONT></P>
<P align=center>
<A
href="Student_Entry.html">INSERT</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<A
href="Student_update_select.asp">UPDATE</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<A href="Student_delete_select.asp">DELETE</A>
</P>
<hr>
<table align="center" border="1" width="40%" bgcolor="#fff5ee">
<tr>
<th align='center' bgcolor='#b0c4de'>Click To Edit</th>
<th align='center' bgcolor='#b0c4de'>Enrollment No</th><th align='center'
bgcolor='#b0c4de'>Student Name</th><th align='center' bgcolor='#b0c4de'>College
Name</th>
<tr>

Enrollment No: Batch: 4th Sem IT Page No:


<%
'Loop through the recordset
Do While not rsstudent.EOF
%>
<tr>
<td>
<% Response.Write ("<a href=""Student_update_form.asp?ID=" & rsstudent("S_Id") &
""">")%>
<%Response.Write ("Click Here")%>
</td>
<td>
<% Response.Write (rsstudent("Enrollment_No")) %>
</td>
<td>
<% Response.Write (rsstudent("Student_Name")) %>
</td>
<td>
<% Response.Write (rsstudent("College_Name")) %>
</td>
</tr>
<%
rsstudent.MoveNext

Loop
%>
<%
'Reset server objects
rsstudent.Close
Set rsstudent = Nothing
Set adoCon = Nothing
%>
</table>
</body>
</html>

5. Student_update_form.asp
<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsstudent 'Holds the recordset for the record to be updated
Dim strSQL 'Holds the SQL query for the database
Dim lngRecordNo 'Holds the record number to be updated

'Read in the record number to be updated


lngRecordNo = CLng(Request.QueryString("ID"))

'Create an ADO connection odject


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

'Set an active connection to the Connection object using a DSN-less connection

Enrollment No: Batch: 4th Sem IT Page No:


adoCon.Provider="Microsoft.Jet.OLEDB.4.0"
adoCon.Open "c:/webdata/Students.mdb"

'Set an active connection to the Connection object using DSN connection


'adoCon.Open "DSN=guestbook"

'Create an ADO recordset object


Set rsstudent = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT student.* FROM student WHERE S_Id=" & lngRecordNo

'Open the recordset with the SQL query


rsstudent.Open strSQL, adoCon
%>
<html>
<head>
<title>Student Update Form</title>
</head>
<body bgcolor="white" text="black">
<!-- Begin form code -->
<form name="form" method="post" action="Student_update_entry.asp">
<P align=center><FONT size=5><U><STRONG>Student
Entry</STRONG></U></FONT></P>
<table align=center border=1>
<tr>
<td><b>Enter Enrollment No :</td>
<td><input type="text" name="eno" maxlength="20" value="<% =
rsstudent("Enrollment_No") %>"></td>
</tr>
<tr>
<td><b>Enter Your Name:</td>
<td><input type="text" name="name" maxlength="60" value="<% =
rsstudent("Student_Name") %>"></td>
</tr>
<tr>
<td><b>Enter College Name:</td>
<td><input type="text" name="clg" maxlength="60" value="<% =
rsstudent("College_Name") %>"></td>
</tr>
</table><br>
<input type="hidden" name="ID_no" value="<% = rsstudent("S_Id") %>">
<center><input type="submit" name="Submit" value="Submit"><center>
</form>
<!-- End form code -->
</body>
</html>
<%
'Reset server objects
rsstudent.Close

Enrollment No: Batch: 4th Sem IT Page No:


Set rsstudent = Nothing
Set adoCon = Nothing
%>

6. Student_update_entry.asp
<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsUpdateEntry 'Holds the recordset for the record to be updated
Dim strSQL 'Holds the SQL query for the database
Dim lngRecordNo 'Holds the record number to be updated

'Read in the record number to be updated


lngRecordNo = CLng(Request.Form("ID_no"))

'Create an ADO connection odject


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

'Set an active connection to the Connection object using a DSN-less connection


adoCon.Provider="Microsoft.Jet.OLEDB.4.0"
adoCon.Open "c:/webdata/Students.mdb"

'Set an active connection to the Connection object using DSN connection


'adoCon.Open "DSN=guestbook"

'Create an ADO recordset object


Set rsUpdateEntry = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT student.* FROM student WHERE S_Id=" & lngRecordNo

'Set the cursor type we are using so we can navigate through the recordset
rsUpdateEntry.CursorType = 2

'Set the lock type so that the record is locked by ADO when it is updated
rsUpdateEntry.LockType = 3

'Open the tblComments table using the SQL query held in the strSQL varaiable
rsUpdateEntry.Open strSQL, adoCon

'Update the record in the recordset


rsUpdateEntry.Fields("Enrollment_No") = Request.Form("eno")
rsUpdateEntry.Fields("Student_Name") = Request.Form("name")
rsUpdateEntry.Fields("College_Name") = Request.Form("clg")

'Write the updated recordset to the database


rsUpdateEntry.Update

Enrollment No: Batch: 4th Sem IT Page No:


'Reset server objects
rsUpdateEntry.Close
Set rsUpdateEntry = Nothing
Set adoCon = Nothing

'Return to the update select page incase another record needs deleting
Response.Redirect "Student_update_select.asp"
%>

7. Student_delete_select.asp
<html>
<head>
<title>Delete Entry</title>
</head>
<body bgcolor="white" text="black">
<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsstudent 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database

'Create an ADO connection odject


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

'Set an active connection to the Connection object using a DSN-less connection


adoCon.Provider="Microsoft.Jet.OLEDB.4.0"
adoCon.Open "c:/webdata/Students.mdb"

'Set an active connection to the Connection object using DSN connection


'adoCon.Open "DSN=guestbook"

'Create an ADO recordset object


Set rsstudent = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT student.* FROM student;"

'Open the recordset with the SQL query


rsstudent.Open strSQL, adoCon
%>

<P align=center><FONT size=5><U><STRONG>Select Enrollment No For Delete Record


</STRONG></U></FONT></P>
<P align=center>
<A
href="Student_Entry.html">INSERT</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<A
href="Student_update_select.asp">UPDATE</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Enrollment No: Batch: 4th Sem IT Page No:


<A href="Student_delete_select.asp">DELETE</A>
</P>
<hr>
<table align="center" border="1" width="40%" bgcolor="#fff5ee">
<tr>
<th align='center' bgcolor='#b0c4de'>Click To Delete</th>
<th align='center' bgcolor='#b0c4de'>Enrollment No </th><th align='center'
bgcolor='#b0c4de'>Student Name</th>
<th align='center' bgcolor='#b0c4de'>College Name</th>
<tr>
<%
'Loop through the recordset
Do While not rsstudent.EOF
%>
<tr>
<td>
<%Response.Write ("<a href=""Student_delete_entry.asp?ID=" & rsstudent("S_Id") &
""">")%>
<%Response.Write ("Click Here")%>
</td>
<td>
<%
Response.Write (rsstudent("Enrollment_No"))
%>
</td>
<td>
<% Response.Write (rsstudent("Student_Name")) %>
</td>
<td>
<% Response.Write (rsstudent("College_Name")) %>
</td>
</tr>
<%
'Move to the next record in the recordset
rsstudent.MoveNext

Loop
%>
<%
'Reset server objects
rsstudent.Close
Set rsstudent = Nothing
Set adoCon = Nothing
%></table></body></html>

Enrollment No: Batch: 4th Sem IT Page No:


8. Student_delete_entry.asp
<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsDeleteEntry 'Holds the recordset for the record to be deleted
Dim strSQL 'Holds the SQL query for the database
Dim lngRecordNo 'Holds the record number to be deleted

'Read in the record number to be deleted


lngRecordNo = CLng(Request.QueryString("ID"))

'Create an ADO connection odject


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

'Set an active connection to the Connection object using a DSN-less connection


adoCon.Provider="Microsoft.Jet.OLEDB.4.0"
adoCon.Open "c:/webdata/Students.mdb"

'Set an active connection to the Connection object using DSN connection


'adoCon.Open "DSN=guestbook"

'Create an ADO recordset object


Set rsDeleteEntry = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT student.* FROM student WHERE S_Id=" & lngRecordNo

'Set the lock type so that the record is locked by ADO when it is deleted
rsDeleteEntry.LockType = 3

'Open the recordset with the SQL query


rsDeleteEntry.Open strSQL, adoCon

'Delete the record from the database


rsDeleteEntry.Delete

'Reset server objects


rsDeleteEntry.Close
Set rsDeleteEntry = Nothing
Set adoCon = Nothing

'Return to the delete select page incase another record needs deleting
Response.Redirect "Student_delete_select.asp"
%>

Enrollment No: Batch: 4th Sem IT Page No:


Outputs
1. Student_Entry.html

2 add_student.asp

3 Student_Data.asp(After Insert)

Enrollment No: Batch: 4th Sem IT Page No:


4. Student_update_select.asp(Before Update)

5. Student_update_form.asp

6 Student_update_select.asp(After Update)

Enrollment No: Batch: 4th Sem IT Page No:


7. Student_delete_select.asp(Before delete)

8. Student_delete_select.asp(After Delete)

Enrollment No: Batch: 4th Sem IT Page No:

You might also like