Creating a News management system (Part 6)

The following code will make up 'del_all.asp' which will show all the news records.


<%@ Language="VBScript" %>
<% Option Explicit %>
<%
'Our login protection code
If Session("BlnLoggedIn") <> True Then
Response.Redirect("login.asp")
End If
%>
<html>
<head><title>News Management System - Delete News</title>
</head>
<body>
<!--#include file="admin_navigation.asp" -->
<%
'declare your variables
Dim Connection, Recordset, sConnString
Dim SQL

'Create ADO connection and recordset object
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("../db/news.mdb")

'Open the connection to the database
Connection.Open(sConnString)

'declare SQL statement that will query the database
SQL ="SELECT * FROM tblNews"


'Open the recordset object, execute SQL statement & return the records
Recordset.Open SQL , connection

'Check to see if there are records with the End of File (EOF) property
If recordset.Eof Then
Response.write "<div align='center'>Sorry there are no current records.</div>"
Else
'loop through the records until we reach the end
Do while not recordset.Eof
response.write recordset("headline") & "<br>"
response.write recordset("news_story") & "<br>"
response.write recordset("story_date") & "<br>"
%>
<a href="delete_record.asp?id=<%= recordset("ID") %>">delete record</a><br><br>
<%
'move on to the next record
recordset.movenext
Loop
End If

'Now that we have displayed the table data lets close the connection
'and the recordset
recordset.Close
Set recordset = Nothing
connection.Close
Set connection = Nothing
%>
</body>
</html>

delete records

Save 'del_all.asp' in the 'admin' folder. In the screenshot above notice the news details and the hyperlink which is circled in red for each news record. Attached to the end of this URL we will be passing a value in the querystring which is the unique ID of the news story. This was assigned automatically to the record once it was entered into the database. This helps us to uniquely identify that record. Once you click on the hyperlink that ID is sent to the linked page. The linked page will then receive and process the information and delete the record with that ID. Simple!

Have a look at the code below for the 'delete_record.asp'

<%@ Language="VBScript" %>
<% Option Explicit %>
<%
'Our login protection code
If Session("BlnLoggedIn") <> True Then
Response.Redirect("login.asp")
End If
%>
<html>
<head><title>News Management System - Delete Records</title>
</head>
<body>
<!--#include file="admin_navigation.asp" -->
<%
'declare your variables
Dim Connection, sConnString
Dim SQL, ID

'get the id of the record sent through the querystring of the hyperlink
ID=request.querystring("ID")

'Create an ADO connection object
Set Connection = Server.CreateObject("ADODB.Connection")

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("../db/news.mdb")

'Open the connection to the database
Connection.Open(sConnString)

'declare SQL statement that will query the database
'and which will be deleting the record with the id sent
SQL="DELETE * FROM tblNews WHERE ID=" & ID

'execute the SQL statement
Connection.execute(SQL)

response.write "<div align='center'>The record was deleted.</div>"

'close the connection
connection.Close
Set connection = Nothing
%>
</body>
</html>

Save 'delete_record.asp' in the admin folder. Notice in the code above we don't use the recordset object as we are not returning any records. As well as being able to delete records learn how to update records using the querystring.

<<Previous

Get the best asp hosting provider from web-hosting-top.com and save up to 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Global ASP.NET Hosting Leader - Click Here