Please can anyone explain clearly....

what is the difference between session and httpcontext.current.session and application variables?

Thankyou in advance...

Recommended Answers

All 12 Replies

Could you tell me in which language, Java, .Net, PHP.

in my friend.

The only difference is how they behave if a session doesn't exist.

Session will throw an exception if the session doesn't exists, whereas HttpContext.Current.Session will return null.

If a session exists, they will both point to the same reference.

@ Ketsuekiame,
Fine and what is the difference between the application and session?

Sorry I don't understand your question now.

Application and Session are completely different types of objects with no similarities at all.

EDIT: Nevermind I think I understand, you mean the data store Application state.

Ok the important difference between the two is that Session stores its data per user. So if you have two users, Bob and Alice, when Bob makes a request, it cannot access Alice's session data.

Application on the other-hand, is a single datastore for the entire application, in-proc (in procedure/memory). So anyone who makes a request can access any data here made by any other user's request.

So in short;

Use Application to store small amounts of global data (such as global site settings etc)
Use Session to store User specific data (such as a shopping basket)

nice explanation... I need a suggestion bro...

My problem is to pass data from one form to another.

Till now i was using session or cookies. But now I need to pass nearly 20 to 25 variables.

I heard that storing in session will reduce the performance of the application.

So I am in search of an efficient idea.

Can you (@Ketsuekiame) help me? Or anyone having any other suggestions???

Thank you.

Store them on the form in a serialised format in a hidden control

Store them on the form in a serialised format in a hidden control


Ok... You mean passing to data to a hidden field and extract them using a string splitter concept... Am I right?

And one more thing...
What about application variable?

I wouldn't use the Application context, at best use the Session context. What I mean is, have a class such as:

public class FormRequestData
    public int MyInteger;
    public bool IsThisTrue;
    public string DescriptionText;

You can serialise this using the Binary Serialiser:

FormRequestData data = new FormRequestData
    MyInteger = 5,
    IsThisTrue = true,
    DescriptionText = "Hi!"

IFormatter formatter = new BinaryFormatter();
Stream myStream = new MemoryStream();
formatter.Serialize(myStream, data);

string base64Data = Convert.ToBase64String(myStream.ToArray());

You can then put that string either, on the form or into the session. Note that if you put it onto the form, you should validate this data on the next page as it could have been tampered with.

Thank you. I hope this helps. As I was waiting for the reply, I found out the get() can be used with the combination of PreviousPage. but the problem with that is the data from one page can be accessed only on the immediate next page. But According to you suggestion, the data can be used anywhere within the applicaion..

Am I right on my statement?

If you put it into the session, you can grab it anywhere yes. If you put it on the form, you will need to retrieve it from the form values and place it onto the next form ad infinitum until you need to use it.

Either way is acceptable really, but if you want to use it throughout the application, it would be better sitting in Session.

K then. I will go with session itself.

Thankyou @Ketsuekiame

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.