Showing posts with label static. Show all posts
Showing posts with label static. Show all posts

Monday, March 26, 2012

Session variable in static method creates error.

Hi. I have a static method that adds a number to a session variable. But when I code a line like this:

Session["CartSubTotal"] = Convert.ToDouble(Session["CartSubTotal"]) + Price;

I get an error like this:

c:\inetpub\wwwroot\aaagolfballs\ShoppingCart.aspx.cs(235): An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Session'

What is a guy to do?If Session["CartSubTotal"] has not been set, that is the error you get.

Check if Session["CartSubTotal"]==null, if so, just set it equal to price rather than trying to add in the existing value of the session variable...
It was null, but I put into the global.asax:


protected void Session_Start(Object sender, EventArgs e)
{
Session["CartSubTotal"] = 0;
}

I still get the same error.
I've got it. For those with the same error, here is how I fixed it. I used these variables in the static method:
System.Web.HttpContext.Current.Session
System.Web.HttpContext.Current.Server

Thanks to all.

Saturday, March 24, 2012

Session variables

Hi,
Basically I see two options:
1- Use application variables, with a Hashtable and the userID is the key.
2- Use a class with a static Hashtable or similar struct. it will be global
to the application therefore will not expire with the section.
Please note that both solutions assume that you are doing this for
registered users, if you allow anonymous users and treat each session as a
different user you have to do something else.
Cheers,
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Bonj" <anonymous@dotnet.itags.org.discussions.microsoft.com> wrote in message
news:B3D56FFC-CC10-495D-9C62-450DED7D33B2@dotnet.itags.org.microsoft.com...
> I see Session variables time out after 20 minutes (or whatever number of
minutes you define).
> I want to have some variables that won't time out, but they only occupy
very small amounts of data, say 10-20 bytes per user, so I think it won't
too much of a performance impact to store it in the web server's memory.
Would having hash tables as global variables using the SessionID as key work
OK? Global variables and SessionIDs don't time out do they?"Bonj" <anonymous@.discussions.microsoft.com> wrote in message news:B3D56FFC-CC10-495D-9C62-
450DED7D33B2@.microsoft.com...
> I see Session variables time out after 20 minutes (or whatever number of m
inutes you define).
> I want to have some variables that won't time out, but they only occupy very small
amounts of data, say 10-20 bytes per user, so I
think it won't too much of a performance impact to store it in the web serve
r's memory. Would having hash tables as global variables
using the SessionID as key work OK? Global variables and SessionIDs don't ti
me out do they?
It's not the session *variables* that time out, it's the *session* that will
be removed (along with all stored values) after the user has done
nothing (=no new page request) for 20 minutes.
How long do you want to keep this data? Could you store it in
a database?
Hans Kesting
All Session variables expires after 20 (or x minutes you choose) whatever
type they have, also the corresponding SessionID of the expired Session. Use
Application variables to store common variables.
Horatiu Ripa
"Bonj" <anonymous@.discussions.microsoft.com> wrote in message
news:B3D56FFC-CC10-495D-9C62-450DED7D33B2@.microsoft.com...
> I see Session variables time out after 20 minutes (or whatever number of
minutes you define).
> I want to have some variables that won't time out, but they only occupy
very small amounts of data, say 10-20 bytes per user, so I think it won't
too much of a performance impact to store it in the web server's memory.
Would having hash tables as global variables using the SessionID as key work
OK? Global variables and SessionIDs don't time out do they?
You could also look at storing this data in an encrypted cookie. Then you
can set the timeout to whatever you wish with no server overhead.
-mike
MVP
"Bonj" <anonymous@.discussions.microsoft.com> wrote in message
news:B3D56FFC-CC10-495D-9C62-450DED7D33B2@.microsoft.com...
>I see Session variables time out after 20 minutes (or whatever number of
>minutes you define).
> I want to have some variables that won't time out, but they only occupy
> very small amounts of data, say 10-20 bytes per user, so I think it won't
> too much of a performance impact to store it in the web server's memory.
> Would having hash tables as global variables using the SessionID as key
> work OK? Global variables and SessionIDs don't time out do they?

Tuesday, March 13, 2012

session variables and global.aspx.cs - where do i put them?

if i put an array in

public class Global : System.Web.HttpApplication
{

//e.g.

public static int [] count=new int[5]

}

and then refer to it in my page .cs files as Global.count[0]... [4] will
every session in my application be accessing the same count array or will
they each have their own?

I guess i am really saying is, is there a difference between the above and
:

protected void Session_Start(Object sender, EventArgs e)
{

Sesson["count"]=new int[5]

}

It strikes me that if they are the same then the first option is better
because i don't have to keep casting from object to int everytime i access
the array in web pages.Hi Tony,

You should think of the Application scope the same way you would think of Environment variables.
They are not only global, but they are also readable and writable to all users of the system at the same
time.

Session is better to use if the values are not final or need to be changed per user.
A session is created for each user making a request.

____________________________________
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Business | Personal

Session Variables and Static Variables

I have found it very easy to have a Helper class that has static
members which access Session Variables. For example:

public class HelperClass
{
public static doCustomer CurrentCustomer
{
get
{
if(HttpContext.Current.Session["Current_Customer"] == null)
CurrentCustomer = new doCustomer();

return (doCustomer)
HttpContext.Current.Session["Current_Customer"];
}
set
{
HttpContext.Current.Session["Current_Customer"] = value;
}
}
}

Is this thread safe? Will each user of the Application still have his
own instance of doCustomer, even though it is accessed via an
Application Wide static member? What is the best practice regarding
Thread Safety, Session Management and Static Members?

ThanksAnybody?