Creating a Simple Hit Counter with ASP.NET 4.0 and C#
This tutorial will demonstrate how to create a simple system to count hits on pages using a database with ASP.NET 4.0 and C#.
STEPS :
Just follow the steps and get result easily.
STEP-1 : CREATE NEW PROJECT
Go to File > New > Project > Select asp.net web Form Application> Entry Application Name > Click OK.
STEP-2: ADD A DATABASE.
Go to Solution Explorer > Right Click on App_Data folder > Add >New Item> Select SQL Server Database Under Data > Enter Database name > Add.
GO
/****** Object: Table [dbo].[tbl_Hits] Script Date: 06/03/2014 16:04:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_Hits](
[Hit_Id] [int] NULL,
[Hit_Name] [nvarchar](50) NULL,
[Hits] [nvarchar](50) NULL
) ON [PRIMARY]
GO
STEP-4:ADDING THE COONCETING STRING
<connectionStrings>
<add name="ErpConnection" connectionString="Data Source=MANVENDRA;Initial Catalog=Meditaion;integrated security=true" providerName="System.Data.SqlClient" />
</connectionStrings>
STEP-5:DESIGN PAGE
<%@ PAGE LANGUAGE="C#" AUTOEVENTWIREUP="TRUE" CODEFILE="INDEX.ASPX.CS" INHERITS="INDEX" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">
<HTML XMLNS="HTTP://WWW.W3.ORG/1999/XHTML">
<HEAD RUNAT="SERVER">
<TITLE></TITLE>
<SCRIPT>
VAR $J = JQUERY.NOCONFLICT();
</SCRIPT>
</HEAD>
<BODY>
<FORM ID="FORM1" RUNAT="SERVER">
<DIV>
<ASP:LABEL ID="LBLHITS" FORECOLOR="WHITE" RUNAT="SERVER" TEXT="LABEL"></ASP:LABEL>
</FORM>
</BODY>
</HTML>
STEP-6: Write the followings code in your page_Load event for fetch Data from Database.
IF (!PAGE.ISPOSTBACK)
{
IF (SESSION["A"] == NULL)
{
CONN = NEW SQLCONNECTION(WEBCONFIGURATIONMANAGER.CONNECTIONSTRINGS["ERPCONNECTION"].CONNECTIONSTRING);
CMD = NEW SQLCOMMAND("UPDATE TBL_HITS SET HITS = HITS+1 WHERE HIT_NAME=@HIT_NAME", CONN);
CMD.COMMANDTYPE = COMMANDTYPE.TEXT;
CMD.PARAMETERS.ADDWITHVALUE("@HIT_NAME", "HOME");
USING (CONN)
{
CONN.OPEN();
CMD.EXECUTENONQUERY();
SESSION["A"] = "SOME VALUE";
}
}
}
CONN = NEW SQLCONNECTION(WEBCONFIGURATIONMANAGER.CONNECTIONSTRINGS["ERPCONNECTION"].CONNECTIONSTRING);
CMD = NEW SQLCOMMAND("SELECT * FROM TBL_HITS WHERE HIT_NAME=@HIT_NAME", CONN);
CMD.COMMANDTYPE = COMMANDTYPE.TEXT;
CMD.PARAMETERS.ADDWITHVALUE("@HIT_NAME", "HOME");
USING (CONN)
{
CONN.OPEN();
SQLDATAREADER RDR = CMD.EXECUTEREADER();
IF (RDR.READ())
{
LBLHITS.TEXT = "TOTAL VISITOR:" + RDR["HITS"].TOSTRING();
}
}
STEPS :
Just follow the steps and get result easily.
STEP-1 : CREATE NEW PROJECT
Go to File > New > Project > Select asp.net web Form Application> Entry Application Name > Click OK.
STEP-2: ADD A DATABASE.
Go to Solution Explorer > Right Click on App_Data folder > Add >New Item> Select SQL Server Database Under Data > Enter Database name > Add.
GO
/****** Object: Table [dbo].[tbl_Hits] Script Date: 06/03/2014 16:04:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_Hits](
[Hit_Id] [int] NULL,
[Hit_Name] [nvarchar](50) NULL,
[Hits] [nvarchar](50) NULL
) ON [PRIMARY]
GO
STEP-4:ADDING THE COONCETING STRING
<connectionStrings>
<add name="ErpConnection" connectionString="Data Source=MANVENDRA;Initial Catalog=Meditaion;integrated security=true" providerName="System.Data.SqlClient" />
</connectionStrings>
STEP-5:DESIGN PAGE
<%@ PAGE LANGUAGE="C#" AUTOEVENTWIREUP="TRUE" CODEFILE="INDEX.ASPX.CS" INHERITS="INDEX" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">
<HTML XMLNS="HTTP://WWW.W3.ORG/1999/XHTML">
<HEAD RUNAT="SERVER">
<TITLE></TITLE>
<SCRIPT>
VAR $J = JQUERY.NOCONFLICT();
</SCRIPT>
</HEAD>
<BODY>
<FORM ID="FORM1" RUNAT="SERVER">
<DIV>
<ASP:LABEL ID="LBLHITS" FORECOLOR="WHITE" RUNAT="SERVER" TEXT="LABEL"></ASP:LABEL>
</FORM>
</BODY>
</HTML>
STEP-6: Write the followings code in your page_Load event for fetch Data from Database.
IF (!PAGE.ISPOSTBACK)
{
IF (SESSION["A"] == NULL)
{
CONN = NEW SQLCONNECTION(WEBCONFIGURATIONMANAGER.CONNECTIONSTRINGS["ERPCONNECTION"].CONNECTIONSTRING);
CMD = NEW SQLCOMMAND("UPDATE TBL_HITS SET HITS = HITS+1 WHERE HIT_NAME=@HIT_NAME", CONN);
CMD.COMMANDTYPE = COMMANDTYPE.TEXT;
CMD.PARAMETERS.ADDWITHVALUE("@HIT_NAME", "HOME");
USING (CONN)
{
CONN.OPEN();
CMD.EXECUTENONQUERY();
SESSION["A"] = "SOME VALUE";
}
}
}
CONN = NEW SQLCONNECTION(WEBCONFIGURATIONMANAGER.CONNECTIONSTRINGS["ERPCONNECTION"].CONNECTIONSTRING);
CMD = NEW SQLCOMMAND("SELECT * FROM TBL_HITS WHERE HIT_NAME=@HIT_NAME", CONN);
CMD.COMMANDTYPE = COMMANDTYPE.TEXT;
CMD.PARAMETERS.ADDWITHVALUE("@HIT_NAME", "HOME");
USING (CONN)
{
CONN.OPEN();
SQLDATAREADER RDR = CMD.EXECUTEREADER();
IF (RDR.READ())
{
LBLHITS.TEXT = "TOTAL VISITOR:" + RDR["HITS"].TOSTRING();
}
}
Comments
Post a Comment