Saturday 22 December 2012

How to Count Website Visitors in Asp.net


Hi Friends,
In this article i will tell you how to count total number of visitors in website by using simple 3 steps
This is a very simple website user counter to see how many people have visited a web page if you want to show it on a web page.

Step 1:  
 We can do this by using (or creating) a global.asax file.
Every time a session is started, you increment an integer, which holds the number of current visitors. Every time a session is ended, you subtract one from that integer. Next, you will have to use public properties for all pages to be able to retrieve the information.


public class Global : System.Web.HttpApplication
{
    private static int totalNumberOfUsers = 0;
    private static int currentNumberOfUsers = 0;

        protected void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
       totalNumberOfUsers = 0;
    }


    protected void Session_Start(Object sender, EventArgs e)
    {
        // Code that runs when a new session is started
      totalNumberOfUsers += 1;
      currentNumberOfUsers += 1;
    }
    protected void Session_End(Object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
       // is set to InProc in the Web.config file. 
        //If session mode is set to   StateServer
        // or SQLServer, the event is not raised.

      currentNumberOfUsers -= 1;
    }
    protected void Application_End(Object sender, EventArgs e)
    { }

    public static int TotalNumberOfUsers{ get { return totalNumberOfUsers; } }
    public static int CurrentNumberOfUsers{ get { return currentNumberOfUsers; } }
}

Step 2:  

Next, in your control, set the values of the global.asax file in the right labels.
private void Page_Load(object sender, System.EventArgs e)
{
  int currentNumberOfUsers = HitCounters.Global.CurrentNumberOfUsers;
  int totalNumberOfUsers = HitCounters.Global.TotalNumberOfUsers;
  lblCurrentNumberOfUsers.Text = currentNumberOfUsers.ToString();
  lblTotalNumberOfUsers.Text = totalNumberOfUsers.ToString();
}


Step 3:  

   In order for this to work correctly we need to enable sessionstate and configure its mode to InProc value (in our web.config file):

        <system.web>
        <sessionState mode="InProc" cookieless="false" timeout="20" />
        </system.web>

In-process mode stores session state values and variables in memory on the local Web server. 
It is the only mode that supports the Session_OnEnd event that we used previously.

Timeout value (in minutes, not in seconds) configures how long our sessions are kept 'alive' - in other words
here we set how long our users can be inactive before considered Offline.

In this example timeout is set to 20 minutes, so while our visitors click on some links in our website at least once
in a 20 minutes, they are considered online.



Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal

Wednesday 19 December 2012

What’s new in Visual Studio 2012


What’s new in Visual Studio 2012

Hi Friends,
Yesterday I install the newest version of Visual Studio which is Visual Studio 2012. The new Visual Studio 2012 family of products includes the commercial Visual Studio 2012 Professional, Premium, Ultimate and the Team Foundation Server 2012.

Following are some of the new features available with this new release of Visual Studio 2012.
  • User Interface - The User Interface is slightly different when compared to previous versions of Visual Studio.

 

User Interface

  • Windows 8 Metro Style Applications - Can create Windows 8 Metro Style applications and this is one of most featured things in Visual Studio 2012 and not to mention it is one of things I am missing in Visual Studio 2010.
  • LightSwitch - LightSwitch for Visual Studio 2012 – Previously Microsoft Visual Studio LightSwitch was a separate product and with the new Visual Studio 2012, it is included in default.
LightSwitch

  • Solution Explorer - Now with the new Solution Explorer we can browse our project’s objects and drill down into methods and properties. It also gives us the ability to search and preview files, objects and external items.
Solution Explorer

  • SQL Server Object Explorer - With previous versions of Visual Studio we couldn’t manage objects in instances of the Database Engine through Visual Studio. Now with the newly introduced SQL Server Object Explorer, it gives us same feeling as using the Object Explorer in SQL Server Management Studio.
SQL Server Object Explorer

  • IIS Express - Previous versions of Visual Studio used its own ASP.NET Development Server as the default web server for locally running and testing web applications. Visual Studio 2012 uses IIS Express as the default local web server. But of course you can change it anytime.
IIS Express

  • Debugging Browser - Visual Studio gives us another nice feature which is, now we can select the browser to debug/browse our web application. For example if your default browser is Google Chrome, your web application will be debugged using Google Chrome in default. But if you are debugging a Silverlight application you specifically need Internet Explorer. Now we don’t have to change the default browser, we only have to change debugging browser.
Debugging Browser




Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal