0

So I need to store some data on my page (webpart) in variables after post request.

But when I try to declare it for example Session["test value"] = someValue;Visual studio just don't recognize Session. I checked namespcses, wrote probably everything that is possible but it still can't see Session.

What am I doing wrong? And if it is not possible for some reason in my case, how should I save Variables after post request? Static class is not an option since he in only 1 for all users and I need to store different data for different users.

1 Answer 1

1

Probably HttpContext.Current.Session is what you need

Example of usage:

HttpContext context = HttpContext.Current;

Store data

context.Session["FirstName"] = firstName;

Retrieve data

firstName = (string)(context.Session["FirstName"]);
6
  • This also doesn't work. 'The type or namespace name 'Httpcontext' could not be found (are you missing a using directive or an assembly reference?)'
    – HoTTab1CH
    Oct 24, 2016 at 9:18
  • Do you have reference to System.Web.Dll? It should be, or you can add it manually.
    – tym32167
    Oct 24, 2016 at 9:20
  • Yes I have, just checked.
    – HoTTab1CH
    Oct 24, 2016 at 9:28
  • so, you have to add using System.Web; in using section.
    – tym32167
    Oct 24, 2016 at 9:33
  • I didn't saw that you edited your answer HttpContext.Current.Session and didn't understand right. But It still doesn't work because I can't just call it like that, HttpContext should be set to something?
    – HoTTab1CH
    Oct 24, 2016 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.