Skip Navigation LinksHome > Categories > Code from a Category

Creating a Simple Hit Counter with ASP.NET 4.0 and C#



User Name: codelecturer
Name: Mike Chauhan
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 3126
Add Date: 11/21/2012
This article will demonstrate how to create a simple system to count hits on individual pages using a database with ASP.NET 4.0 and C#.
Adding the Database

To keep track of the number of hits on each individual page of our sample site, we will need a small database that allows us to associate a number of hits to a given page. At this point in the article I have created a new ASP.NET Empty Web Site. To create this database:

1. Right click the project in your solution epxlorer.
2. Select add ASP.NET folder.
3. Select App_Data.
4. Right click the App_Data folder.
5. Select add new item...
6. Select a SQL Database.
7. Name it 'Database.mdf'.
8. Click add.
9. Expand the Database.mdf folder in your server/database explorer.
10. Right click the Tables folder.
11. Select add new table.
12. Add the following columns with their respective types to the table:

Column Name Data Type
Name nvarchar(50)
Hits int

13. Save the table as 'Hits'.
14. Right click the Hits table.
15. Select Show Table Data.
Add the following rows to the table:

Name Hits
Default 0

About 0

This table will keep track of the hits to our Default and About pages respectively.

Adding the ConnectionString
Now that we have our database setup, the next thing that we need to do is add the connection to it in our Web.Config file. To do this, open up the Web.Config file for editing and add in the following code in between the configuration and system.web tags:



Adding the Default.aspx Page

Next, we need to add a couple of pages to our web site. First we will add the home page, Default.aspx. To do this:

1. Right click the project in your solution explorer.
2. Select add new item...
3. Select a web form.
4. Name it 'Default.aspx'.
5. Click add.
6. Open Default.aspx up to design mode.
7. Drag and drop a hyperlink onto the web form.
8. Change the NavigateUrl property of the hyperlink to '~/About.aspx'.
9. Change the text of the hyperlink to 'About'.
10. Add a break line after the hyperlink.
11. Drag and drop a label underneath the break line.
12. Change the ID property of the label to 'lblHits'.
13. Change the Text property of the label to an empty string.
14. Adding the About.aspx Page

Next, we will add the about page, About.aspx. To do this:

1. Right click the project in your solution explorer.
2. Select add new item...
3. Select a web form.
4. Name it 'About.aspx'.
5. Click add.
6. Open About.aspx up to design mode.
7. Drag and drop a hyperlink onto the web form.
8. Change the NavigateUrl property of the hyperlink to '~/Default.aspx'.
9. Change the text of the hyperlink to 'Home'.
10. Add a break line after the hyperlink.
11. Drag and drop a label underneath the break line.
12. Change the ID property of the label to 'lblHits'.
13. Change the Text property of the label to an empty string.


Counting the Hits

Next, we need to add in some code that will increment the number of hits associated with each page when they are loaded. First, we will add the functionality to the Default.aspx page. To do this, open up Default.aspx.cs for editing and add in the following using statements:

Next, add the following code to the Page_Load event method:


Let's review what this code is doing. First, it determines if the page load method is being triggered by a postback or not. If it is not a postback, we increment the hits in our table that correspond to the Default.aspx page. Then, we grab the number of hits from the database and display that in a label so that we can see the current number of hits. Next, we need to add similar code to the About.aspx page. To do this, open up About.aspx.cs for editing and add in the following using statements:


Next, add the following code to the Page_Load event method:


Testing
To test this out, load up the web site and click back and forth on the links. Notice the number of hits displayed in the label go up. Refresh the page a few times to increase the count on one page and ensure that you have two different respective counts for each page.

Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.