banner Hi, this is Shiva Kumar. You can call me Shiva. I'm a developer like you, with 7 Years of experience on different technologies. Earlier I used to write articles for Computers Today. Now I started this web site to help each other ( or ) atleast contribute some thing from my end. I'm always a student, if you need any articles or help which is not there in this web site, please send me a mail. I will update you as soon as I can.
Jul
4th

Counting Number of Users on the Web Site - ASP.net,VB.net,C#.net

Author: admin | Files under Asp.net, tutorials

Introduction

Most of the web sites today using the hit count either to track the number of users (or) to display on the web sites to increase the popularity. There are many ways and each have pros and cons. In this article, I will try to show the different ways of counting and showing the number of users as follows:

  • Using the Application variables(Global.asax)
  • Using the text files
  • Displaying the user count as a colorful image on the web sites

Using the Application variables (Global.asax )

Most of programmers, who think about counting number of users on the web site, first thing, that strikes in mind, are using the application variables to hold the user count. Because, it is simple to implement and can be accessed application wide. Let first see how to do this:

First open the global.asax file and go to session_start() function. Now add the following code:

  1.  
  2. protected void Session_Start(Object sender, EventArgs e)
  3. {
  4. Application[TOTALNUMBEROFUSERS] = Convert.ToInt32(Application[TOTALNUMBEROFUSERS]) + 1;
  5. }
  6.  

Use the application state variable TOTALNUMBEROFUSERS to access the count and display it to the users in your asp pages. This seems to be simple but, this approach has a major drawback, like, if the application restarts, the count will be lost because, the application variables are initialized again when the application starts.

Using the text files (Not lose User Count Between Application Restarts)

As discussed, storing user count on application variables is not a good option. We need some way of storing the user count, so even if the application restarts, we can access user count. The simple solution is using the text files to store the user count as follows:

  1.  
  2. Try
  3. If not File.Exists(sFile) then
  4.         objWriter = File.CreateText(β€œcount.txt”)
  5.         objWriter.Write("0")
  6.         objWriter.Close
  7. End if
  8.  
  9. objReader = File.OpenText(sFile)
  10. sCount = objReader.ReadToEnd()
  11. objReader.Close
  12. iCount = Cint(sCount)
  13. iCount = iCount + 1
  14. sCount = iCount.ToString
  15.  
  16. objWriter = File.CreateText(sFile)
  17. objWriter.Write(sCount)
  18. objWriter.Close
  19.  
  20. Catch Ex as Exception
  21. Label1.width = New Unit(640)
  22. sCount = "[Count could not be obtained due to the following error]: " &  Ex.Message
  23.  
  24. Finally
  25.  
  26. HitCountLabel.Text = β€œThe total number of hits: ” & sCount
  27.  
  28. End Try
  29.  

Put the above code in the page_load function, so when the page is loaded the function executes each time and updates the user count.

Displaying the user count as a colorful image on the web sites

The above section describes how to save and retrieve the hit counter from the text file. By using the same hit counter, I will try to show to how to create a image with the number of users as follows:
[Continuation of the above example]

  1.  
  2.  
  3. Dim bitmap as new Bitmap(200,150,
  4.            System.Drawing.Imaging.PixelFormat.Format32bppArgb)
  5. Dim  g as Graphics= Graphics.FromImage(bitmap)
  6. Dim pen as Pen new Pen(Color.Yellow)
  7. Dim rect as Rectangle new Rectangle(0,0,200,150)
  8. Dim b as SolidBrush new SolidBrush(Color.DarkKhaki)
  9. Dim blue as SolidBrush new SolidBrush(Color.Blue)
  10. Dim counter as integer
  11. Dim i as integer
  12.  
  13. counter =0
  14. i=0
  15. g.DrawRectangle(pen, rect)
  16. g.FillRectangle(b, rect)
  17.  
  18. for  i = 0 to sCount.Length - 1
  19.    
  20.         g.DrawString(sCount[i],
  21.         new Font("Verdena", 10 + rand.Next(14, 18)),
  22.         blue, new PointF(10 + counter, 10))
  23.        
  24. next i
  25.  
  26. Response.ContentType = "image/gif"
  27. bitmap.Save(Response.OutputStream,ImageFormat.Gif)
  28. g.Dispose()
  29. bitmap.Dispose()
  30.  

Similar Posts - 90% of Users have seen these posts also

Post a Comment