ASP.NET 2.0 Everyday Apps For Dummies (2006)
.pdfChapter 11: Building a Blog Application 379
The Comments page
When the user clicks the View Comments link in the Blog page, the Comments page is displayed, as shown in Figure 11-4. This page displays any comments left by other users. The comments are listed in descending date sequence, so the most recent comment is displayed first. A query string field named postid is used to determine the post whose comments are to be displayed. (The other query string fields shown in the URL for this page, post and blogid, are used so to remember which blog and post to display when the user returns to the Blog page. (There’s more to know about these query string fields; you can see it in the source code for the Blog.aspx page.)
This page also includes a Return link that returns to the Blog page and a
Leave Comment link that lets the user add a new comment.
Figure 11-4:
The
Comments
page.
The Leave Comment page
If the user clicks the Leave a Comment link on the Blog page or the Comments page, the Leave Comment page is displayed (as shown in Figure 11-5). Here the user can leave a comment that’s associated with a particular post. The Leave Comment page has text boxes for the user to enter his or her name and comment. Note that the user doesn’t have to register or log in to leave a comment. As a result, the user can enter any name he or she pleases in the Name text box.
Chapter 11: Building a Blog Application 381
Figure 11-6:
The Login
page.
The Register page
To create a new user account, the user can click the Register link that appears in the Master Page. Then the Register page appears, as shown in Figure 11-7. This page uses an ASP.NET CreateUserWizard control to create a new user account. (For more information about this control, refer to Chapter 4.)
The My Blogs page
Figure 11-8 shows the My Blogs page, which is displayed when the user clicks the My Blogs link that appears in the Master Page and has logged in. This page displays the blogs the user has created, lets the user post a new article to one of his or her blogs, and lets the user create a new blog. To post a new article to a blog, the user simply clicks the New Post link for the blog. To create a new blog, the user enters the blog’s name and description and clicks the Create Blog button.
Chapter 11: Building a Blog Application 383
The New Post page
The New Post page is shown in Figure 11-9. This page is displayed when the user clicks the New Post link on the My Blogs page. It lets the user enter the subject and text for a new article to be posted to the user’s blog. Note that the blog the article should be posted to is passed to the New Post page as a query string field.
Figure 11-9:
The New
Post page.
Designing the Database
The database for the Blog application uses just three tables:
Blogs
Posts
Comments
Figure 11-10 shows a diagram for the database, and the following sections describe each table individually.
386 Part V: Building Community Applications
Table 11-3 (continued)
Column name |
Type |
Description |
commentdate |
DATETIME |
The date and time the comment was |
|
|
recorded. The default value for this |
|
|
column is the current date and time. |
|
|
|
username |
VARCHAR(100) |
The name of the person who left the |
|
|
comment. |
|
|
|
comment |
VARCHAR(MAX) |
The text of the comment. |
|
|
|
Creating the Database
You can create the Blog database by running the script that’s shown in Listing 11-1. To run this script, open a command prompt window and change to the directory that contains the script. Then enter this command:
sqlcmd -S localhost\SQLExpress -i CreateBlogDB.sql
As usual, you’ll need to change the host name from localhost if you’re not using SQL Server Express on your own computer.
Listing 11-1: The CreateBlogDB.sql script
USE master |
|
|
1 |
GO |
|
|
|
IF EXISTS(SELECT * FROM sysdatabases |
2 |
||
WHERE name=’Forum’) |
|
|
|
DROP DATABASE Blog |
|
|
|
GO |
|
|
|
CREATE DATABASE Blog |
|
3 |
|
ON (NAME=Product, |
|
|
|
FILENAME = ‘C:\APPS\Blog.mdf’, |
|
||
SIZE=10 ) |
|
|
|
GO |
|
|
|
USE Blog |
|
|
4 |
GO |
|
|
|
CREATE TABLE Blogs ( |
|
5 |
|
blogid |
INT IDENTITY, |
|
|
username |
VARCHAR(100) |
NOT NULL, |
|
name |
VARCHAR(255) |
NOT NULL, |
|
|
|
|
|