how to increment Application Object in Global.asax file
-
hi i am using Application Object to count the number of users created for a webpage.I am using following code in C#:
-
protected void Session\_Start(object sender, EventArgs e)
-
{
-
int userCount=(int)(Application\["userCount"\]) ;
-
Application\["userCount"\] = ++userCount;
-
}
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..!!!"
-
-
hi i am using Application Object to count the number of users created for a webpage.I am using following code in C#:
-
protected void Session\_Start(object sender, EventArgs e)
-
{
-
int userCount=(int)(Application\["userCount"\]) ;
-
Application\["userCount"\] = ++userCount;
-
}
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..!!!"
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.
-
-
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.
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..!!!"
-
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..!!!"
As indeed it is. However your item in the Application may not have been added, so it will not exist.
-
hi i am using Application Object to count the number of users created for a webpage.I am using following code in C#:
-
protected void Session\_Start(object sender, EventArgs e)
-
{
-
int userCount=(int)(Application\["userCount"\]) ;
-
Application\["userCount"\] = ++userCount;
-
}
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..!!!"
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()
andApplication.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 datavoid 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
-
-
hi i am using Application Object to count the number of users created for a webpage.I am using following code in C#:
-
protected void Session\_Start(object sender, EventArgs e)
-
{
-
int userCount=(int)(Application\["userCount"\]) ;
-
Application\["userCount"\] = ++userCount;
-
}
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..!!!"
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 -
-
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 TemplatesAbhishek 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..!!!"