HomeASP.NET Build an ASP.NET 3.5 Guestbook using MS SQ...
Build an ASP.NET 3.5 Guestbook using MS SQL Server and VB.NET
One of the most important website features is a “guest book.” This is particularly useful if you need to know the responses and reactions of your website's visitors. With the release of ASP.NET 3.5 and Visual Web Developer Express 2008, several web controls make it possible to create an ASP.NET application without the need to “hard/manually code” everything, including database scripts, server side scripts, etc. You can see how that would be helpful to writing a guest book. This is the first part of a multi-part series.
Indeed, this efficient development approach is highly useful for developing complicated web applications such as a guest book. ASP Free already features past tutorials on developing guest books using ASP.NET technology. However, since that tutorial was written in 2004, and ASP.NET technology was not as advanced and "efficient" as it is today (and neither was the MS SQL server database), it seems like a good time to revisit the topic. In the 2004 tutorial, after all, development relied more on hard/manual coding that made it look very complicated, especially for a beginner to developing ASP.NET websites.
This tutorial has been written using the latest available Microsoft web technology as of April 2010:
ASP.NET 3.5
Visual Web Developer Express 2008
Visual Basic.NET 2008
MS SQL Server 2008.
So if you have these requirements installed on your Windows computer, you can start following this tutorial and develop your own ASP.NET 3.5 guest book web application.
The overall objective of this tutorial is to present a "user-friendly" or "click and drag" technique for developing ASP.NET guest books, so that even ASP.NET 3.5 beginners (with some background in form handling, web controls and validation) can follow this tutorial without the need to do "hard coding" or manually writing the code from scratch.
Before discussing the details that will let us move forward, you will need to consider how the guest book application can be constructed using the available tools from latest ASP.NET web technology. For example, you will need to display the guest book as designed below:
This guest book accepts three things from the user: name, email address and a short message (which is at most 1000 characters). To prevent spamming bots from abusing your guest book, you need to have a Recaptcha added to your form. All submitted messages will then appear below the guest book in the form of a table with three columns: name, message and date.
The "date" will consist of the date and time the message is submitted to the server and the database.
Of course, since you cannot trust user input and some users might enter incorrect information, you will need to validate the guest book inputs before they can be written to the database.
Sketching the layout on a piece of paper, and given the above application requirements, you might come out with the strategy below (imagining the web controls that are to be added in the "Design" view tab of Visual Web Developer Express):
Important note: Before even proceeding to step 1, you need to create a "new" project using Visual Web Developer Express. Once the default files of the project have been created, you can proceed to create the database, its tables and its associated default data for the guest book application. After completing these prerequisites, proceed to step 1 below.
Step 1 (#1 in the screen shot above). You need to add a date and time label web control. This is generated using a server side script (Visual Basic). This will show the real server time and display it on the browser. The purpose of this is that the ID of this label web control will be "bound" later to the SqlDataSource INSERT statement for "date." This way, that any "successful" submission to the database will use the current server date and time and stored on the dataandtime field (to be discussed later).
Step 2. SqlDataSource should be added to the web form, because other web controls for gathering user input as well as displaying the records in the web browser need to connect and interact with the database. The SqlDataSource should be able to perform two important functions. First, it needs to handle SQL SELECT statements, which are used for retrieving records in the guest book database and displaying them on the browser. And second, it needs to handle SQL INSERT statements, used for inserting new records (such as a new guest book entry) into the MS SQL server database.
Step 3. A form is needed to gather the user name, valid email and the message. You can use the "DetailsView" web control for inserting records into the database, and configure its "default" to accept inputs such as shown in the screen shot earlier. User input validation will be applied to all of these required fields to ensure the correctness of the data before it is inserted into the database. Also, this DetailsView web control interacts with the database to INSERT records and will depend on the SqlDataSource configured in Step 2.
Step 4. Since the DetailsView defaults to an "Insert" link, it will be changed to a button with the text "Submit." This is more user-friendly and understandable for common Internet users.
Step 5. To prevent spam and automatic guest book form submission, a recaptcha web control will be added. An ASP.NET library for Recaptcha will need to be installed and integrated with your guest book application. Do not worry; this is simple and will be discussed in the coming sections.
Step 6. The last step is to show the table showing the previous guest book comments. Since it is a table that shows an entire database of table records, the "gridview" web control is appropriate for showing the data.
Of course, since gridview needs to communicate with the database, it needs to depend on the SqlDataSource control configured in step 2.
After steps 1 through 6 are completed, it is time to add some finishing touches to the guest book web application, such as applying formatting, colors, font style, and so forth.
You will need to create a new project using Visual Web Developer using the steps outlined at the following link (step 1 to step 4). You'll use the following information:
Project name: guestbookapplication
Visual Studio Installed Templates: ASP.NET Web Site
Location: File system
Path: %Windowspath%guestbookapplication
Replace %Windowspath% to your preferred Windows path where you would like to save the projects. For example if I save my project in E:aspdotnetprojects folder, then the path to be entered will be:
E:aspdotnetprojectsguestbookapplication
Language: Visual Basic
Finally, click OK. Visual Web Developer creates the default files (Default.aspx, App_data folder, etc). Proceed to Step 5 and 6 of this tutorial, and then follow the rest of it, which explains how to create an ASP.NET database.
Use the parameters listed below in creating the database, the tables and its data:
Database name: guestbook.mdf
The important database table fields (there are five):
1. guestid
2. name
3. email
4. message
5. dateandtime
The specifications for each of these fields when creating them in a table are in the screen shot below:
Database table name: guestbooktable
Important note: Do not forget to set "guestid" as the primary key index (PMI) in the guestbooktable. Refer to the tutorial link provided for details.
Enter those sample records; they are just examples, so you can edit the messages to anything you need (you can even shorten them). After doing this, the guestbooktable will look like this screen shot:
Please refer to the second part of this tutorial to start the discussion of the first step and the rest of the development process.