Click here to Skip to main content
Licence 
First Posted 16 Jan 2003
Views 513,966
Downloads 4,602
Bookmarked 49 times

ASP.NET Guestbook using MS Access

By Dilbert2004 | 16 Mar 2004
Shows an easy way of building a guestbook using ADO.NET and Access
6 votes, 31.6%
1
2 votes, 10.5%
2

3
2 votes, 10.5%
4
9 votes, 47.4%
5
3.32/5 - 19 votes
μ 2.59, σa 3.25 [?]

Sample Image - myaspnetguestbook.jpg

Introduction

This project shows an easy way to create a guestbook built using ASP.NET. Access database is used to store the data. ADO.NET is used to access the data on the server. To format the data, I use the Repeater control that comes with Visual Studio .NET.

Background

The guestbook is split into two pages, one where the user can write in the guestbook and the other shows a log of all the guestbook entries.

Using the code

In order to be able to access data through a website, you'll have to include these two lines of code on every page you want to use data access methods:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>

I'm first going to describe the one where the user writes to the guestbook. The code needed to create the connection to the database looks like this:

sub OnBtnSendClicked (s As Object, e As EventArgs)
    Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _ 
                                    & server.mappath("guestbook.mdb") & ";" 
    Dim MySQL as string = "INSERT INTO Guestbook " & _ 
       "(Name, EMail, URL, Comment) VALUES " & _
       "('" & txtName.Text & "','" & txtEMail.Text & "','" _ 
       & txtURL.Text & "','" & txtComment.Text & "')" 
    Dim MyConn as New OleDBConnection (strConn) 
    Dim cmd as New OleDBCommand (MySQL, MyConn) 
    MyConn.Open () 
    cmd.ExecuteNonQuery () 
    MyConn.Close () 
    Response.Redirect ("guestlog.aspx") 
end sub

This function executes when the user selects the "Send" button. It creates a connection with the server and then adds what the user typed in the form to the database, using the INSERT INTO statement. The txtName.Text retrieves the context of the Name field and adds it to the command. The other fields are retrieved exactly the same. You can see the code for the form in the source file, that comes with this article.

After the function has added the new record, the user is redirected to the log page, where he can see all the other entries in the guestbook. Now we are going to look at the page, that displays the entries of the guestbook (database).

This function executes whenever the page is loaded (or refreshed). It creates a connection with the database, and binds the data to the Repeater control. The Repeater control is formatted elsewhere in the file, a great way to separate data and logic.

Sub Page_Load (Source As Object, E as EventArgs)
    Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _ 
                                & server.mappath("guestbook.mdb") & ";"
    Dim MySQL as string = "SELECT Name, EMail, URL, Comment FROM Guestbook"
    Dim MyConn as New OleDBConnection (strConn)
    Dim Cmd as New OleDBCommand (MySQL, MyConn)
    MyConn.Open ()
    rptGuestbook.DataSource = _ 
      Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    rptGuestbook.DataBind()
End Sub

You can see the code for the Repeater in the source file, but one interesting thing that I used is to automatically create a link to the website the user provided in the form. That is done using the Hyperlink control that comes with Visual Studio .NET.

Points of Interest

I hope this article has shown you how easy it is to create a simple guestbook in a very short time. Of course, you may like to format the output differently. I didn't spend much time on the design of the interface, but instead concentrated on the logic. I haven't included any error checking, in order to make the code as simple as possible.

If you understand the theory behind this guestbook, you can move on to some more complex things using ASP.NET. I hope you enjoyed this as much as I have! - Good luck!

Update 16.03.2004

This article talks about the "Operation must use an updateable query" problem, that many people are having.

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

About the Author

Dilbert2004

Web Developer

Iceland Iceland

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralError Code Pinmemberseangheng21:40 19 Jul '09  
GeneralASP.NET login page Pinmemberkholiwe23:28 4 Apr '07  
GeneralRe: ASP.NET login page Pinmemberhafizakahbk16:46 24 Jul '07  
QuestionHelp Pinmemberasifahaniff6:38 20 Aug '06  
GeneralBlank Log view page Pinmemberfredtbx5:08 19 Jan '06  
GeneralRe: Blank Log view page PinmemberGabriel8212:23 19 Apr '07  
GeneralNext Page Pinmembershlvy7:50 12 Aug '04  
GeneralSecurity PinmemberJeffrey Sax20:01 17 Mar '04  
Nicely done!
 
I would like to point out a security issue in your code as it stands: SQL code injection.
 
In a nutshell: because you are building your SQL statement by hand (not using a stored procedure), and you are pasting the contents of the form controls straight into your SQL statement, you are leaving an opening for a malicious guest to run arbitrary SQL code on your database.
 
A short discussion can be found on CP here[^]. MSDN also did a Webcast[^] on the subject.
 
Jeffrey

Everything should be as simple as possible, but not simpler.
    -- Albert Einstein

http://www.extremeoptimization.com/
GeneralRe: Security PinmemberTony Truong13:40 18 Mar '04  
GeneralRe: Security PinmemberJeffrey Sax18:18 18 Mar '04  
GeneralRe: Security PinmemberTony Truong9:28 22 Mar '04  
GeneralRe: Security PinsussAnonymous23:03 27 May '05  
GeneralRunTimeError Pinmembersashy6:39 28 Feb '04  
GeneralRe: RunTimeError PinmemberArniG11:47 4 Mar '04  
GeneralAdding a search option to this Pinmemberdal20617:22 13 Apr '03  
GeneralGot errors Pinmemberxiaosong17:52 17 Feb '03  
GeneralRe: Got errors PinmemberTiger Woods22:57 18 Feb '03  
GeneralRe: Got errors Pinmemberxiaosong12:11 19 Feb '03  
GeneralRe: Got errors PinsussAnonymous9:01 14 Jun '03  
GeneralRe: Got errors PinsussAnonymous9:16 14 Jun '03  
GeneralRe: Got errors PinsussMichelle_ho4:41 8 Mar '04  
GeneralRe: Got errors PinmemberArniG3:41 16 Mar '04  
GeneralOther similar articles PinsitebuilderUwe Keim2:06 18 Jan '03  
Questionwrong category? PinmemberSteve McLenithan10:48 17 Jan '03  
AnswerRe: wrong category? + ... PinmemberSteve McLenithan10:51 17 Jan '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 17 Mar 2004
Article Copyright 2003 by Dilbert2004
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid