This is the first part of the ASP.NET Forums Website tutorial series. This tutorial will introduce you to what a forum is and guide you through the steps of creating the master page and database.

What is a forum?

A forum is essentially a message board that is organized into topics, which allow users to make posts to these topics for everyone to see. Each post can contain any number of comments from our users. Our forum will consist mainly of three things, Topics, Posts, and Comments.

  • Topics – These will be dictated by us when we create the site. Each Topic will contain some number of posts by our users.
  • Posts – A post will be created by a user, within a topic. A post contains a title, and a message which becomes the first comment on the post. Each post can contain any number of comments.
  • Comments – A comment will be created by a user, within a post. A comment contains a message that is appended to the end of the other comments on the post it is applied to.

Let’s take an example of this. For instance, our forum has a topic titled ASP.NET Discussion. Within this topic, a user can make a post with a title such as, ‘Help on Web Forms’ and add a message like, ‘I need help adding a Web Form to my website’. Then, another user can locate that post and add a comment to it saying ‘Simply click add>new item>web form’.

What pages will we need?

We will need 5 different pages, all based off of a master page, to create our forum.

  • Default.aspx – This will be the home page and will list links and info to the various topics in our forum.
  • Topic.aspx – This page will display links to all of the posts within a given topic as well as a link to add a new post to the current topic you are viewing.
  • NewPost.aspx – This page will allow the user, if logged in, to add a new post to a given topic.
  • Post.aspx – This page will display all of the comments for a given post and will also display a link to add a new comment on the given post.
  • NewComment.aspx – This page will allow the user, if logged in, to add a new comment to a given post.

What data will we need to store?

We will need to create a database that will store all of the data associated with our forums, and allow us to both organize the data and associate the data with topics, posts, and comments. To do this, we will be creating a database with 3 different tables.

  • Topics – This table will contain all of the data for our topics and will contain 5 columns
    • TopicId – This will be a unique identifier that is associated with the topic.
    • TopicName – This will be the name of the topic that the user sees.
    • LastPostTitle – This will display the title of the last post posted in this topic.
    • LastPostUserName – This will display the username of the person who posted last.
    • LastPostDateTime – This will display the time and date of the last post.
  • Posts – This table will contain all of the data for every post made and will contain 5 columns.
    • >PostId – This will be a unique identifier that is associated with the post.
    • TopicId – This id will correspond to the TopicId of the topic that the post is posted in.
    • PostTitle – This will be the title of the post that users will see.
    • PostUserName – This will display the name of the user who created the post.
    • PostDateTime – This will display at what time the post was made.
  • Comments – This table will contain all of the data for every comment made and will contain 5 columns.
    • CommentId – This will be a unique identifier that is associated with the comment.
    • PostId – This id will correspond to the PostId of the post that this comment was added to.
    • CommentUserName – This will display the name of the user who added the comment.
    • CommentMessage – This will display the message of the comment added.
    • CommentDateTime – This will display at what time the comment was made.

Creating our Master Page
At this point in the tutorial I have created a new ASP.NET Empty Web Site. What we want to do first is create a Master Page with a title and link to the home page. To do this:

  1. Right click the project in your Solution Explorer.
  2. Click Add New Item…
  3. Select Master Page, keeping the default name MasterPage.master.
  4. Click Add.

The next thing we need to do here is add some content to the page. To do this, open up the MasterPage to Design mode and:

  1. Select somewhere above the ContentPlaceHolder1.
  2. From the top menu select Table -> Insert Table.
  3. Create a table with 2 rows and 2 columns, and a width of 100% and hit OK.
  4. Select the 2 cells in the top row and merge them together.
  5. Add in the text ‘ASP.NET Forums Tutorial’ and change the font size to xx-large.
  6. In the bottom left cell, drag and drop a Hyperlink Control and change the Text property to ‘Home’ and the NavigateUrl property to ‘Default.aspx’.
  7. In the bottom right cell, drag the ContentPlaceHolder1, which will be where the content from our pages show up on the master page. 

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

Adding the Database
The next thing we want to do is setup our database. To do this:

  1. Right click the project in your Solution Explorer.
  2. Select Add ASP.NET Folder and click App_Data.
  3. Right click the App_Data folder and select Add New Item…
  4. Select a SQL Server Database, name it ForumDB and click Add.

This should take you to the Server or Database Explorer which shows all of the different folders within the database we have added. What we want to do next is add in some tables to hold our data. The first table we are going to add is the Topics table. To do this, right click the Tables folder and select Add New Table. Create the following columns with their respective types:

Column Name Data Type
TopcId int
TopicName varchar(50)
LastPostTitle varchar(255)
LastPostUserName varchar(50)
LastPostDateTime smalldatetime

Save the table as Topics. Next, we need to set the TopicId to the primary key of this table. To do this:

  1. Right click the TopicId row and select Set Primary Key.
  2. Under the Column Properties, set the Is Identity property under Identity Specification to Yes.

The next table we will add is the Posts table. To do this, right click the Tables folder and select Add New Table. Create the following columns with their respective types:

Column Name Data Type
PostId int
TopicId int
PostTitle varchar(255)
PostUserName varchar(50)
PostDateTime smalldatetime

Save the table as Posts. Next, we need to set the PostId to the primary key of this table. To do this:

  1. Right click the PostId row and select Set Primary Key.
  2. Under the Column Properties, set the Is Identity property under the Identity Specification to Yes.

The next table we will add is the Comments table. To do this, right click the Tables folder and select Add New Table. Create the following columns with their respective types:

Column Name Data Type
CommentId int
PostId int
CommentUserName varchar(50)
CommentMessage varchar(MAX)
CommentDateTime smalldatetime

Save the table as Comments. Next, we need to set the CommentId to the primary key of this table. To do this:

  1. Right click the CommentId row and select Set Primary Key.
  2. Under the Column Properties, set the Is Identity property under the identity Specification to Yes.

Once this is setup we are ready to begin designing our web pages.

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

The MasterPage.master source looks like this:

[freebiessub download = “http://aspnettutorials.com/AtomsNetwork/Files/60/ASPdotNET-Forums-Website-Tut.zip”>Download Source Files