Wednesday, 16 January 2013

Create Page Hit Counter in Asp.Net

   
Create Page Hit Counter in Asp.Net

Create table Query for Hit Counter

CREATE TABLE [dbo].[hits](
    [i_autoid] [int] IDENTITY(1,1) NOT NULL,
    [hits] [float] NULL,
    [cratedate] [datetime] NULL CONSTRAINT [DF_hits_cratedate]  DEFAULT (getdate())
)

C# Code For Hit Counter

public partial class _Default : System.Web.UI.Page
    {

        dataclass dc = new dataclass();

        protected void Page_Load(object sender, EventArgs e)
        {
            // select max hits from table
            string query = "select max(hits) as hits from hits";
            DataSet ds = new DataSet();
            ds = dc.connection(query);
            float hit = float.Parse(ds.Tables[0].Rows[0]["hits"].ToString());
            //increment hit counter with 1
            float hitsnew =    hit+1;
            // update hit counter table
            string query1 = "UPDATE  hits SET  hits = '"+hitsnew+"'";
            dc.execquery(query1);
        }
    }

We are using just simple steps.on pageload we select data from table and update counter after increment, when pageload this will increment the counter and you can show it in label ttextbox or any where..
DC is the object of Class where we perform sql related task like create connection etc.
executequery method execute query to sql server.you can use your way to execute queries.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.