ASP.NET Content Rater Web Site Part 1 Databases
This is part one of the ASP.NET Content Rater Web Site series. In this tutorial we will begin setting up the web form and databases using ASP.NET 4.0 and C#.
This tutorial is the first part of a series that will be walking you through step by step how to create a simple web site with content that your users can apply ratings to. For this example, we will use a database with some books in it and allow the user to associate a rating between 1 to 5 for each of those books respectively.
First, we will need to go ahead and create a web site with users enabled and create an account that we will use to test the ratings out with. To do this, create a new ASP.NET Empty Web Site and:
- Click the ASP.NET Configuration icon in your solution explorer.
- Click the security tab.
- Click select authentication type.
- Select from the internet.
- Click done.
- Click create user.
- Fill out the form and click create user.
- Close the ASP.NET Web Site Administration Tool.
Now that we have enabled users and have created an account to test our web site with, we need to add the database that will hold all of our books and ratings. To do this:
- Click the refresh icon in your solution explorer.
- Notice the App_Data folder was added when we enabled user creation.
- Right click the App_Data folder.
- Select add new item…
- Select a SQL server database.
- Name it ‘Database.mdf’.
- Click add.
This database will need to hold two tables, one to hold our book data and a second to hold our user’s ratings. To add the books table:
- Expand your Database.mdf folder in your server/database explorer.
- Right click the Tables folder.
- Select Add New Table.
- Add the following columns with their respective types:
Column Name Data Type BookId int Title nvarchar(255) Author nvarchar(50)
- Right click the BookId column.
- Select Set Primary Key.
- Change the IsIdentity property of the BookId column to ‘Yes’.
- Save the table as ‘Books’.
Next, we need to add some sample data into the books column so our users will have something to rate. To do this:
- Right click the Books table.
- Select Show Table Data.
- Add in the following books:
BookId Title Author 1 The Adventures of Huckleberry Finn Mark Twain 2 The Grapes of Wrath John Steinbeck 3 The Great Gatsby F. Scott Fitzgerald 4 Moby Dick ;Herman Melville 5 A Tale of Two Cities Charels Dickens 6 To Kill a Mockingbird Harper Lee 7 Wuthering Heights Emily Bronte
Next, we will need to add in a page to our web site that will have a login control for the user to login and also display the books for our users to rate. To do this:
- Right click the project in your solution explorer.
- Select add new item…
- Select a web form.
- Name it ‘Default.aspx’.
- Click add.
- Drag and drop a loginview control onto the web form.
- Add the following text to the anonymoustemplate of the loginview: ‘Login to view your ratings.’
- Drag and drop a login control into the anonymoustemplate.
- Add a few break lines after the loginview.
- Drag and drop a repeater after the break lines.
This form will only display a login control if the user is not logged in. Under the login control, we have added a repeater which we will use to display the books with their appropriate ratings. This concludes part one of the tutorial, we have setup a database and a form that we will use to add in the rest of our functionality so the users can apply their own ratings to the books in our database.
Download Source Files