ASP Pages - Deleting Records
Page
2 of 2
Deleting Data from a Database
This page contains no HTML so we can start writing the asp straight away, still don't forget the server side script tags, <% .... %>.
First we need to dimension the variables used in the script, so open your text editor and enter the following code.
|
<%
Dim adoCon
Dim rsDeleteEntry
Dim strSQL
Dim lngRecordNo
|
|
Next we need to get the 'ID Number' of the entry to
be deleted from the database. This ID number was passed to the page we are
writing by the script we wrote at the beginning of the session, by adding the '?ID=
Entry ID Number' at the end of the URL.
To read in the ID number we are going to use the 'QueryString'
method of the ASP 'Request' object. We're also going
to use the 'CLng' VBScript function to convert the
ID number to the data type, 'Long Integer'.
|
lngRecordNo =
CLng(Request.QueryString("ID"))
|
|
Next we need to create a database connection object on the server using the ADO
Database connection object.
|
Set adoCon =
Server.CreateObject("ADODB.Connection")
|
|
Now we need to open a connection to the database.
|
adoCon.Open "DRIVER={Microsoft
Access Driver (*.mdb)}; DBQ=" &
Server.MapPath("guestbook.mdb")
adoCon.Open "DSN=guestbook"
|
|
Next create an ADO recordset object which will hold the records from the
database.
|
Set rsDeleteEntry =
Server.CreateObject("ADODB.Recordset")
|
|
To query a database we need to use SQL (Structured Query Language). In the next
line we initialise the variable 'strSQL' with an SQL
query to read in all the fields from the 'tblComments'
table where the 'ID_no' = the entry to be deleted,
this way the query will only return one record to the recordset.
|
strSQL = "SELECT tblComments.* FROM
tblComments WHERE ID_no=" &
lngRecordNo
|
|
Because we are going to be deleting the record held in the recordset we need to
set the LockType of the recordset to 'adoLockOptimistic'
so that the recordset is locked when it is deleted. The integer value for this
lock type is 3.
|
rsDeleteEntry.LockType = 3
|
|
Now we can open the recordset and run the SQL query on the database to get the
database entry that we want to delete.
|
rsDeleteEntry.Open strSQL, adoCon
|
|
Once the recordset is open and contains the entry we want to delete, we can
delete the entry from the database by using the 'Delete'
method of the 'Recordset' object.
We have finished using the database in this script so we can now close the
recordset and reset the server objects.
|
rsDeleteEntry.Close
Set rsDeleteEntry = Nothing
Set adoCon = Nothing
|
|
Now that the database record has been deleted we are going to use the 'Redirect'
method of the ASP response object to redirect back to the page we wrote at the
beginning of this session, 'delete_select.asp' so
that another entry can be selected to be deleted from the database. Note that if
you are going to use the 'Response.Redirect' method
you must remember to redirect before any HTML is written.
|
Response.Redirect "delete_select.asp"
%>
|
|
Name the file 'delete_entry.asp' and save it to the
same directory as the Guestbook database. Don't forget the '.asp'
extension.
<<
Back
Main
Page | Hosting
Plans | E-Commerce
| Sign
Up Now! | Site
Map
asp
web hosting by HyperlinkHost.com