Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how to increment Application Object in Global.asax file

how to increment Application Object in Global.asax file

Scheduled Pinned Locked Moved ASP.NET
csharpquestionasp-netvisual-studiohelp
7 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QuickDeveloper
    wrote on last edited by
    #1

    hi i am using Application Object to count the number of users created for a webpage.I am using following code in C#:

    1.   protected void Session\_Start(object sender, EventArgs e)
      
    2.  {
      
    3.       int userCount=(int)(Application\["userCount"\]) ;
      
    4.       Application\["userCount"\] = ++userCount;
      
    5.   }
      

    It gives error @ line 3 saying Object reference not set to an instance of an object. in WebPage. how do i increment Application Object Value in Session_Start? i tried

    Application["userCount"] + =1

    but it says Can't convert Int to Object type.... I am using VS 2008 /ASP.NET 3.5 thanx in advance..

    "Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"

    P A A 3 Replies Last reply
    0
    • Q QuickDeveloper

      hi i am using Application Object to count the number of users created for a webpage.I am using following code in C#:

      1.   protected void Session\_Start(object sender, EventArgs e)
        
      2.  {
        
      3.       int userCount=(int)(Application\["userCount"\]) ;
        
      4.       Application\["userCount"\] = ++userCount;
        
      5.   }
        

      It gives error @ line 3 saying Object reference not set to an instance of an object. in WebPage. how do i increment Application Object Value in Session_Start? i tried

      Application["userCount"] + =1

      but it says Can't convert Int to Object type.... I am using VS 2008 /ASP.NET 3.5 thanx in advance..

      "Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"

      P Offline
      P Offline
      Paddy Boyd
      wrote on last edited by
      #2

      Does your Application["userCount"] item exist the first time this code has run? Your code doesn't handle the possibility of it not being there.

      Q 1 Reply Last reply
      0
      • P Paddy Boyd

        Does your Application["userCount"] item exist the first time this code has run? Your code doesn't handle the possibility of it not being there.

        Q Offline
        Q Offline
        QuickDeveloper
        wrote on last edited by
        #3

        i thought Application object is global object and therefore it exists/initialized by default.....thanx for help..i'll check on the web

        "Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"

        P 1 Reply Last reply
        0
        • Q QuickDeveloper

          i thought Application object is global object and therefore it exists/initialized by default.....thanx for help..i'll check on the web

          "Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"

          P Offline
          P Offline
          Paddy Boyd
          wrote on last edited by
          #4

          As indeed it is. However your item in the Application may not have been added, so it will not exist.

          1 Reply Last reply
          0
          • Q QuickDeveloper

            hi i am using Application Object to count the number of users created for a webpage.I am using following code in C#:

            1.   protected void Session\_Start(object sender, EventArgs e)
              
            2.  {
              
            3.       int userCount=(int)(Application\["userCount"\]) ;
              
            4.       Application\["userCount"\] = ++userCount;
              
            5.   }
              

            It gives error @ line 3 saying Object reference not set to an instance of an object. in WebPage. how do i increment Application Object Value in Session_Start? i tried

            Application["userCount"] + =1

            but it says Can't convert Int to Object type.... I am using VS 2008 /ASP.NET 3.5 thanx in advance..

            "Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"

            A Offline
            A Offline
            Abhijit Jana
            wrote on last edited by
            #5

            QuickDeveloper wrote:

            1. protected void Session_Start(object sender, EventArgs e) 2. { 3. int userCount=(int)(Application["userCount"]) ; 4. Application["userCount"] = ++userCount; 5. }

            It is always better to use Application.Lock() and Application.Unlock() before any kind of operation with Application Variable. Because ASP.NET is multithreaded and when multiple users accessing the web site at the same it is required to synchronized the data

            void Session_Start(object sender, EventArgs e)
            {
            if (Application["userCount"] != null) {
            Application.Lock();
            int visitorCount = (int)Application["userCount"];
            Application["userCount"] = userCount++;
            Application.UnLock();
            }
            }

            cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net My Latest Article : IIS Remote Debugging

            1 Reply Last reply
            0
            • Q QuickDeveloper

              hi i am using Application Object to count the number of users created for a webpage.I am using following code in C#:

              1.   protected void Session\_Start(object sender, EventArgs e)
                
              2.  {
                
              3.       int userCount=(int)(Application\["userCount"\]) ;
                
              4.       Application\["userCount"\] = ++userCount;
                
              5.   }
                

              It gives error @ line 3 saying Object reference not set to an instance of an object. in WebPage. how do i increment Application Object Value in Session_Start? i tried

              Application["userCount"] + =1

              but it says Can't convert Int to Object type.... I am using VS 2008 /ASP.NET 3.5 thanx in advance..

              "Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"

              A Offline
              A Offline
              Abhishek Sur
              wrote on last edited by
              #6

              Check before you call Application object placed in the variable:

              if (Application.AllKeys.Contains("userCount"))
              {
              Application.Lock();
              int item = Convert.ToInt32(Application["userCount"]);
              // If you are unsure check type of variable as well
              Application["userCount"] = item + 1;
              Application.UnLock();
              }
              else
              {
              Application.Set("userCount", 1); // Initial Value
              }

              :rose:

              Abhishek Sur My Latest Articles Working with Excel using MDAC
              Basics on LINQ and Lambda Expressions
              Create .NET Templates

              Q 1 Reply Last reply
              0
              • A Abhishek Sur

                Check before you call Application object placed in the variable:

                if (Application.AllKeys.Contains("userCount"))
                {
                Application.Lock();
                int item = Convert.ToInt32(Application["userCount"]);
                // If you are unsure check type of variable as well
                Application["userCount"] = item + 1;
                Application.UnLock();
                }
                else
                {
                Application.Set("userCount", 1); // Initial Value
                }

                :rose:

                Abhishek Sur My Latest Articles Working with Excel using MDAC
                Basics on LINQ and Lambda Expressions
                Create .NET Templates

                Q Offline
                Q Offline
                QuickDeveloper
                wrote on last edited by
                #7

                Abhishek Sur wrote:

                if (Application.AllKeys.Contains("userCount"))

                thanx Abhishek..it really works!!! :)

                "Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups