Nine rapid-fire paragraphs draw out the pertinent details of this listing:
1 Sets the database context to master.
2 Deletes the existing Blog database if it exists.
3 Creates a database named Blog. The data file is stored in
C:\Apps.
4 Sets the database context to Blog.
5 Creates the Blogs table.
6 Creates the Posts table.
7 Creates the Comments table.
8 Creates a trigger named tr_PostCount. This trigger executes whenever a row is inserted in the Posts table. It defines a variable named @blogid which is set to the value of the blogid column in the inserted row. Then it executes an UPDATE statement that increments the posts column for the Blogs row that the new post was added to.
9 Creates a trigger named tr_CommentCount. This trigger executes whenever a row is inserted in the Comments table. It defines a variable named @postid which is set to the value of the postid column in the inserted row. Then it executes an UPDATE statement that increments the comments column of the appropriate row in the Posts table.
Adding test data
On the companion CD, you’ll find a script named InsertData.sql, which creates test data for the database. It creates two blogs, along with several posts and comments.
Chapter 11: Building a Blog Application 389
To run the InsertData.sql script, open a command window, change to the directory that contains the script, and run this command:
sqlcmd -S localhost\SQLExpress -i InsertData.sql
You’ll need to change the server name if it is other than localhost\ SQLExpress.
SQL statements for working with the database
The Blog application uses several SQL statements to retrieve and update data in the Blog database, as described in the following paragraphs:
To list the Blogs on the default.aspx, this SELECT statement is used:
SELECT [blogid], [name],
[description], [username], [posts]
FROM [Blogs]
ORDER BY [name]
The MyBlogs.aspx page uses a similar SELECT statement to retrieve the blogs for the current user:
SELECT [blogid], [name],
[description], [username], [posts] FROM [Blogs]
WHERE [username]=@username ORDER BY [name]
The Blog.aspx page uses the following SELECT statement to list the posts in a particular blog:
SELECT [postid], [blogid], [postdate], [subject]
FROM [Posts]
WHERE ([blogid] = @blogid) ORDER BY [postdate] DESC
The posts are sorted in descending date sequence, so the newest post is listed first.
The Blog.aspx page also uses this SELECT statement to retrieve the details for the selected post:
As with the other applications in this book, the connection string for the Blog application is stored in the <connectionStrings> section of the web.config file:
Note that if you’re not using SQL Server Express on your local computer, you’ll have to modify the connection string to provide the correct server name.
Building the Master Page
The Master page for the Blog application is shown in Listing 11-2. The codebehind file for this Master Page has no methods, so it isn’t shown here.
Here’s a rundown on the key points of this listing:
1 If you’re using Visual Basic rather than C# for the application’s code-behind files, you should change the Language attribute to
C# and the AutoEventWireup attribute to false.
2 The LoginName control displays the name of the user if the user has logged in. The FormatString attribute adds the word Hello before the user name.
3 This link button returns the user to the home page
(Default.aspx).
4 The LoginStatus control displays a link button that lets the user log in or out.
5 This link button posts back to the Register.aspx page so new users can register.
6 This link button posts to the MyBlogs.aspx page. This page is in the Admin folder, which contains a web.config file that prohibits anonymous access. As a result, the user must log in to access this page. If the user is not already logged in, ASP.NET will automatically redirect the user to the Login.aspx page. To accomplish this, the web.config file in the Admin folder contains the following lines:
<system.web>
<authorization>
<deny users=”?” /> </authorization>
</system.web>
7 The ContentPlaceHolder control marks the location where the content for each page of the application will be displayed.
Building the Blog Home Page
The Blog Home page (Default.aspx) displays a list of the blogs that are available on the Blog Web site. You can refer back to Figure 11-2 to see how this page appears when the application runs.
The Blog Home page consists mostly of a GridView control that displays a list of the blogs in the Blogs table. The GridView control displays three columns. The first displays the name and description of the blog, with the name presented as a link button that posts to the Blog.aspx page and passes the ID of the selected blog as a query string field. The second column displays the name of the user that created the blog, and the third displays the number of posts that have been made to the blog.
Listing 11-3 presents the .aspx code for this page. A code-behind file isn’t required.
Seven important lines in this listing merit a little closer look:
1 You should change the Language, AutoEventWireup, and CodeFile attributes in the Page directive if you’re working with Visual Basic rather than C#. (It doesn’t really matter for this page, since no code-behind file is needed, but you should change it nonetheless.)
2 The <Content> element provides the content that’s displayed for the page.
3 The GridView control lists the blogs retrieved from the Blogs table by the SQL data source named SqlDataSource1. Note that paging is enabled for this GridView control. As a result, ten blogs will be listed at a time.
4 The first column defined for the GridView control is a template column. This column specifies two templates:
The header template displays the word “Blog” as the column heading.
The item template displays the blog name and title. The blog name is shown as a link button that uses binding expressions for the Text and PostBackUrl attributes. The binding expression for the Text attribute simply retrieves the name field from the data source. The binding expression for the PostBackUrl attribute is a little more complicated. It retrieves the blogid field from the data source and uses a format string to create a URL that specifies the blogid as a query string field. For example, if the blogid value is 3, the PostBackUrl attribute’s value will be Blog.aspx?blog=3.
5 The second column in the GridView control is bound to the username field in the data source.
6 The third column in the GridView control is bound to the posts field in the data source.
7 The data source uses a SELECT statement to retrieve the blogid, name, description, username, and posts columns from the
Blogs table.
Chapter 11: Building a Blog Application 395
Building the Blog Page
The Blog page displays one post from a selected blog (by default, the most recent) as well as a list of all the posts that have been made to the blog. The Blog page is passed a query string field to indicate which blog to display. In addition, a query string field is used to indicate which post should be displayed. If this query string is not present, the most recent post for the blog is displayed by default.
A code-behind file is used here to determine which post to display based on the post query string and to handle the Click events for the buttons that display comments for the post or enable a visitor to leave a comment. The following sections present the Blog.aspx file and both the C# and Visual Basic versions of the code-behind file.
The Blog.aspx page
Listing 11-4 presents the .aspx code for the Blog page. Most of the code for this page is straightforward, so you shouldn’t have any trouble following it.