Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

News System with ASP

0.00/5 (No votes)
5 Aug 2004 1  
Simple News System using ASP and MS Access

Sample Image - logo.png 

ASP News System

This is a simple news system that can be easily added to any website. Download full asp news source code and instructions.

This code uses the following technolgies

  • ASP (Active Server Pages).
  • MS Access Database.

Be sure that you your system or web host supports these technologies.

Details

Start by editing the settings.asp file, this file contains the basic settings for the news system such as:

  • Your News Title
  • Login Code (password)
  • Number of stories to show per page.
  • Database path
newstitle = "News Demo"
logincode = "1234"
show_stories = "10"
database_path = Server.MapPath("news_data.mdb")

This file also contains a few functions that are used as well as some style settings that can be changed to  customize the look of the news system

login

The login page is a simple html form which is checked against the login code that is set in the settings.asp file. If successful, then it sets the session and continues to the admin area.

<%Option Explicit%>
<!-- #INCLUDE FILE="settings.asp" -->
<%
' Intercept all exceptions to display user-friendly error


if Request.Form("mynumber") = "" then
response.redirect("login-news.asp?l=password_blank")
End If

'set variables from form

FormPwd = Request.Form("mynumber")
FormPwd = replace(FormPwd,"'","''")

'run login or return to login page

if formpwd = logincode then
    Session("LoginID") = formpwd
else
response.redirect("login-news.asp?l=incorrect_login_or_password")
End if

'final redirect

response.redirect("news-admin.asp")
%>

Once logged in the screen looks very similar to the public news screen, but with more administrative options.

asp news

<!-- #INCLUDE FILE="login-check.asp" -->
<!-- #INCLUDE FILE="settings.asp" -->
<%
if not IsNumeric(show_stories) then
    show_stories = "999"
end if

Set dConn = Server.CreateObject("ADODB.Connection")
dConn.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & database_path

showall = request.querystring("showall")

if showall <> "" then
    strSQL = "SELECT TOP " & show_stories & 
    " expire, id, newsdate, newstitle, 
newsbody, active from tblnews order by id desc, newsdate desc;"
    else
    strSQL = "SELECT TOP " & show_stories 
    & " expire, id, newsdate, newstitle, 
newsbody, active from tblnews where active = 1 order by 
    id desc, newsdate desc;"
end if

Set newsRS = Server.CreateObject("ADODB.recordset")
newsRS.Open strSQL, dConn, 1, 3
recordcount = newsRS.recordcount
if recordcount <> 0 then 
    data = newsRS.GetRows()
    'Data is retrieved so close all connections

        newsRS.Close
        Set newsRS = Nothing
        dconn.close
        Set dconn = nothing
    'Setup for array usage

    iRecFirst   = LBound(data, 2)
    iRecLast    = UBound(data, 2)
end if
%>
<!-- #INCLUDE FILE="admin-menu.asp" -->
</head>
<body>
<table border=2 bordercolor="#CCCCFF" CELLSPACING=0 CELLPADDING=4>
<%
if recordcount <> 0 then 
' Loop through the records (second dimension of the array)

For I = iRecFirst To iRecLast
    news_expire = data(0,I)
    news_id = data(1,I)
    news_dat = data(2,I)
    news_title = data(3,I)
    news_body = data(4,I)
    active = data(5,I)
        if active = 1 then
        textcolor = "black"
        else
        textcolor = "gray"
        end if
        if DateDiff("d", todaydate, news_expire) > 0 then
        textcolor = "black"
        else
        textcolor = "gray"
        end if

    response.write "<tr>" & _
                    "<td style='color: " & textcolor & ";'><strong>" & 
news_title & "</strong> <small>date: " & news_dat & " expires: " & 
news_expire & "</small>" & _
    " [<a title='edit news' href='edit-news.asp?id=" & news_id & 
"'>edit</a>] [<a title='delete news' href='news-cmd.asp?id=" & news_id 
& "&delnews=1' onclick=""return confirm('Are you sure you want to 
delete?')"">delete</a>]</td> " & _
                    "</tr>" & _
                    "<tr>" & _
                    "<td>" & news_body & "</td>" & _
                    "</tr>" & vbcrlf
Next ' I

end if
%>

From here you can edit current news stories add new news stories, or delete news stories.

The edit news stories, will basically read the values from the database based on the querystring value passed, and put them into an html form which can then update the database with your changes made.

asp news

<%option explicit%>
<!-- #INCLUDE FILE="login-check.asp" -->
<!-- #INCLUDE FILE="settings.asp" -->
<%

id = request("id")
if id < 1 then
    response.write (errormsg)
    response.end
end if

Set dConn = Server.CreateObject("ADODB.Connection")
dConn.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & database_path

'========================================

if request.form("ispostback") = 1 then
    set updateRS = Server.CreateObject("ADODB.RecordSet")
    updateSQL = "SELECT * from tblnews where ID = " & ID & ";"
    updateRS.Open updateSQL,dConn, 1,3
        urecordcount = updateRS.recordcount
        if urecordcount <> 1 then
            updateRS.Close
            Set updateRS = Nothing
            response.redirect("default.asp?norecord")
        end if
    updateRS("newsdate") = checkdate(request.form("newsdate"))
    updateRS("expire") = checkdate(request.form("expire"))
    updateRS("newstitle") = Presubmit2(request.form("newstitle"))
    updateRS("newsbody") = Presubmit2(request.form("newsbody"))
    updateRS("active") = request.form("active")
    updateRS.update
    updateRS.Close
    set updateRS = Nothing
    message = "<p><font color='#FF0000'>Update Successful</font></p>"
end if
'========================================


strSQL = "SELECT expire, id, newsdate, newstitle, 
  newsbody, active from tblnews where ID = " & ID & ";"
Set newsRS = Server.CreateObject("ADODB.recordset")
newsRS.Open strSQL, dConn, 1, 3

        recordcount = newsrs.recordcount
        if recordcount < 1 then
            newsRS.close
            set newsRS = nothing
            dConn.close
            set dConn = nothing
            response.write (errormsg)
            Response.end
        end if

For fnum = 0 To newsRS.Fields.Count-1
    execute(newsRS.Fields(fnum).Name & " = 
  newsRS(" & CHR(34) & newsRS.Fields(fnum).Name & CHR(34) & ")")
Next
if newsbody <> "" then
    newsbody = undo(newsbody)
end if

%>
<!-- #INCLUDE FILE="admin-menu.asp" -->
<title>Edit News</title>
<h3><%=newstitle%></h3>

<%=message%>

<form action="edit-news.asp" method="post" name="frm" id="frm">
<table border=2 bordercolor="#CCCCFF" CELLSPACING=0 CELLPADDING=4>
<tr>
    <td>News Title</td>
    <td><input type="text" name="newstitle" 
  value="<%=newstitle%>" size="50" class="cssborder"></td>
</tr>
<tr>
    <td>News Date</td>
    <td><input type="text" name="newsdate" 
  value="<%=newsdate%>" size="12" class="cssborder"></td>
</tr>
<tr>
    <td>News Expire Date</td>
    <td><input type="text" name="expire"
   value="<%=expire%>" size="12" class="cssborder"></td>
</tr>
<tr>
    <td valign="top">News Text</td>
    <td><textarea cols="50" rows="8" 
  name="newsbody" class="cssborder"><%=newsbody%></textarea></td>
</tr>
<tr>
    <td valign="top">Active</td>
    <td>
    <% if active = "1" then%>
    <input type="checkbox" name="active" value="1" checked>
    <% else %>
    <input type="checkbox" name="active" value="1">
    <% end if %>
    </td>
</tr>

<tr>
    <td colspan="2">
    <input type="hidden" name="id" value="<%=id%>">
    <input type="hidden" name="ispostback" value="1">
    <input type="submit" value="update" class="cssborder"> 
    <input type="reset" class="cssborder">
    </td>
</tr>
</table>
</form>
<p><a href="edit-news.asp?id=<%=id%>">reload</a></p>

When adding news, the browser simple goes to the news-cmd.asp file, which acts like a utility file, doing multiple functions depending on what value is passed to it.

In this code, you can see where the insert takes place. To make this simple a standard SQL insert is used to populate the news database with a new record which can then be easily edited. You can also see from this file that if a delete command is sent to the file with a record id, then a news item can also be deleted from this page.

<!-- #INCLUDE FILE="login-check.asp" -->
<!-- #INCLUDE FILE="settings.asp" -->
<%
id = request("id")
response.write ID
if not isnumeric(id) then
    response.write errmessage
    response.end
end if

addnews = request("addnews")
delnews = request("delnews")

Set dConn = Server.CreateObject("ADODB.Connection")
dConn.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & database_path

if addnews = 1 then
insertdate = FormatDateTime(Now(), vbShortDate)
    mySQL = "INSERT INTO tblnews(newstitle, newsbody, newsdate, active)
 VALUES ('New News Title', 'New News Text', #" & insertdate & "#, 1);"
    dConn.execute(mySQL)
    redirect = "news-admin.asp"
end if

if delnews = 1 then
    mySQL = "DELETE FROM tblnews where ID = " & id & ";"
    dConn.execute(mySQL)
    redirect = "news-admin.asp"
end if

dconn.close
%>

<script language="javascript">
<!-- 
location.replace("<%=redirect%>");
-->
</script>

Conclusion

Download full asp news source code and instructions. That is about all there is to it, this is just a very simple news system an easy way to keep your site updated from a simple and quick web interface. Hope you enjoy it!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here