ASP 101 - Active Server Pages 101 - Web01
The Place ASP Developers Go!



Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash





ASP.NET version of "Hit Counter (Database)"


Here's a version of our classic hit counter (database) script implemented in ASP.NET. I've implemented it almost exactly the same way I implemented the ASP.NET text file version. In fact, if you look at them, counter.aspx and counter_db.aspx are almost exactly the same. The syntax for using the control doesn't change between the text file and database versions. All the changes are internal to the control.

Speaking of those changes... the first and foremost is that since this counter stores its counts in a database table, you'll need a copy of the table. You can download an MS Access file that illustates the layout from here: counter_db.mdb, but once you look at the code, you'll quickly realize that it's set up to use our SQL Server and not Access. Getting it to use Access should not be difficult, but it's left as an exercise for the reader. (If you want to do this, you may find our plain old ASP.NET Database Sample useful.) For those of you using SQL or any other DB, here's a quick run down of the fields and their setup via a sample table creation script. Even if you don't know how to use it, it should explain enough about the layout for you to recreate it. After all, it's really pretty simple.

CREATE TABLE dbo.hit_count
	(
 	page_name varchar(255) NOT NULL,
	hit_count int NULL
	) ON [PRIMARY]
GO
ALTER TABLE dbo.hit_count ADD CONSTRAINT
	PK_hit_count PRIMARY KEY NONCLUSTERED 
	(
	page_name
	) ON [PRIMARY]
GO

Oh and feel free to change the table or field names. I'm using the same table I originally set up for the classic asp version of the script. If I were doing it today I might name things differently, but I really didn't see any reason to change things at this point.

The zip file below includes the ascx control, a sample aspx file that uses the control, and a copy of the SQL script listed above. You can get the images in a zip file from here(5.3 KB).

Here's a zip file of the code (2.4 KB).

Play with the running version.


Back to the "Hit Counter (Database)" sample


Samples

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers