Login | Register  

ASP.NET Web Counter - Free Open Source Code

by Scud   Saturday, November 26, 2005 Rating:
 Vote this news:


 

Counter can track and display hit of the page(s).  This simple counter uses only one stored procedure to update and display hits. So it uses less server memory. The database back-end is SQL Server. But you may easily modify procedure to use with MS Access.

First run this SQL script to create :

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[counter]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[counter]
GO

CREATE TABLE [dbo].[counter] (
 [hit] [int] NOT NULL
) ON [PRIMARY]
GO
INSERT [dbo].[counter] (hit) VALUES (1)
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[webcounter]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[webcounter]
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE PROCEDURE dbo.webcounter
AS
BEGIN
SET NOCOUNT ON
DECLARE @hits INT
update counter set hit = hit + 1
SELECT hit from counter
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

This is our ASP.NET page. Please modify database connection.

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>

<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)

 Dim objCon As New SQLConnection("server=ASP;User id=SA;password=;database=dotnet")

 Dim cmd As SQLCommand = New SQLCommand("EXEC dbo.webcounter", objCon)
 objCon.Open()
 Dim r as SQLDataReader
 r = cmd.ExecuteReader()
 r.read()
 strCounter.text = "Hit : " & r.item(0)
 end sub
</script>
<html>
<head>
</head>
<body>
<asp:label ID="strCounter" runat="server"/>
</body>
</html>






Dotnetindex.com is listed with 411asp.net
Dotnetindex.com is listed with aspin.com
Dotnetindex.com is listed with jspin.com
PHP, ASP, .NET, JSP Resources, Reviews







page counter
Copyright © 2007 Powered by GNews Publisher Positive. All rights reserved. Contact Us - Load Time: 0.06 second(s)