1

i have problem to get / set the Session variable inside c# classes, its working with MVC actions / asp.net forms but not working with other than Form / MVC Controller classes.

i used this way

System.Web.HttpContext.Current.Session["connectioncount"] = 6;

int a = (int)System.Web.HttpContext.Current.Session["connectioncount"];

reference i used this help

Error

Problem: i need to set / get session variable value in 1 class other than MVC controller class and also access this Session variable value in Library / other 2 class. or i need a variable which will be used through out the application but for each users it will have different values and will clear / set to default when user will disconnect app. its web app

can any one help me, i find more solutions but have problem to use them. i appreciate your valuable time. thanks in advance.

adnan
  • 1,429
  • 1
  • 16
  • 26
  • where you use it? what kind of application it is? console app, windows app? – Vlado Pandžić Jul 22 '17 at 12:06
  • There are several things that can be `null` in that statement you have, could be the `.Current` or the `.Session` or the actual `.Session["...."]`, can you please make sure which one is null by decomposing the statement into it's parts in a watch window? – Ruskin Jul 22 '17 at 12:06
  • its a web application using asp.net MVC, but i need to access Session variable in my Library project and other classes in MVC but other than MVC controller classes. – adnan Jul 22 '17 at 12:11
  • what it means by several things, is this not a .Session only? @Ruskin – adnan Jul 22 '17 at 12:12
  • 1
    `HttpContext.Current` only has meaning inside an HTTP request lifecycle. Elsewhere it will always be `null`. Also, it's a really bad practice to access `HttpContext` outside the scope of an ASP.NET project (you mentioned *other* libraries). Please make sure you are trying to accessing it while a request is executed and not outside of it. – Federico Dipuma Jul 22 '17 at 12:15
  • as i noticed now, HttpContext.Current is null, what other suggestion will be if i need to access Session in other classes. – adnan Jul 22 '17 at 12:17
  • The problem is not *where* you are trying to access to `Session`, but *when*. An `HttpContext` (and thus a `Session`) exists only **inside an HTTP request lifecycle**. To be clear: only when your code is executed due to a HTTP request from a client. – Federico Dipuma Jul 22 '17 at 12:23
  • Bingo @adnan, so it wasn't the session but the fact that there is no `HttpContext`, is this call within an HttpRequest lifecycle as @federico suggested? – Ruskin Jul 22 '17 at 12:24
  • did you see the reference link, where i followed? is this correct or not @Federico Dipuma – adnan Jul 22 '17 at 12:50
  • reference : https://stackoverflow.com/questions/621549/how-to-access-session-variables-from-any-class-in-asp-net – adnan Jul 22 '17 at 12:52
  • 1
    Your link explains *how* to access to a `Session`, but I do not believe you understood *when*. As already said is not *where* but *when*. – Federico Dipuma Jul 22 '17 at 12:54
  • @Federico Dipuma, please can you tell me where i am wrong. thanks – adnan Jul 22 '17 at 13:02
  • I can't, because I do not know when are you calling the code, in which class, created how, and called in which context. – Federico Dipuma Jul 22 '17 at 13:08
  • i need to set / get session variable value in 1 class other than MVC controller class and also access this Session variable value in Library 2 class. or i need a variable which will be used through out the application but for each users it will have different values and will clear / set to default when user will disconnect app, – adnan Jul 22 '17 at 13:12
  • @Federico Dipuma more reference https://msdn.microsoft.com/en-us/library/system.web.httpcontext.session(v=vs.110).aspx – adnan Jul 22 '17 at 13:22

0 Answers0