Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
We using class file, in that i need to access session value which assign in aspx page.
I create one function in class file, when i call that function from server code (button server side click event), i can get session value which assigned in aspx page,
But same function i call from client side (button client side event), i get error like this.
Errorscreenshot : http://prntscr.com/mhtxnz[^]

What I have tried:

For getting session value in class file
Dim strusename As String = System.Web.HttpContext.Current.Session("username")


Regards,
Aravind
Posted
Updated 7-Feb-19 10:54am
Comments
F-ES Sitecore 7-Feb-19 7:26am    
Just post the text of the error message, people are less likely to navigate off to potentially unsafe sites to help you with your problem, or the site might be blocked by corporate firewalls.

Quote:
when i call that function from server code (button server side click event), i can get session value which assigned in aspx page,
But same function i call from client side (button client side event), i get error


Sorry, but i don't understand what you mean by saying: "from client side". If you're able to save session variables, you have to be able to read them.

Note, that Session object is on server side. See:
Quote:
ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests. ASP.NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to persist variable values for the duration of that session. By default, ASP.NET session state is enabled for all ASP.NET applications.

Source: ASP.NET Session State Overview[^]

For further details, please see:
How to: Save Values in Session State[^]
How to: Read Values from Session State[^]
Implementing a Session-State Store Provider[^]
 
Share this answer
 
As I told you last time[^], if you're calling a WebMethod, you need to turn on session state:
[WebMethod(EnableSession = true)]

Without doing that, HttpContext.Current.Session will return null, and you'll get a NullReferenceException.
 
Share this answer
 
Comments
Aravindba 7-Feb-19 23:50pm    
Hi , yes above code for web service(asmx) and yes can get session, but i asked in class file(.vb)
Richard Deeming 8-Feb-19 6:42am    
If you don't enable session state for the handler, then HttpContext.Current.Session will return null.

It doesn't matter whether that line appears directly in the web service, or in a function in another class that you call from the web method; without enabling the session state for the web method, you won't be able to access the session.
you have to check if the session is null first, if it is not, then assign to strusename. The session is an Object and thus, you will need to convert it to string also.


In C#
protected string ApplicationReviewStatus
 {
    get => ViewState["ApplicationReviewStatus"]?.ToString() ?? string.empty;
    set => ViewState["ApplicationReviewStatus"] = value;
 }


Also, you do this in the page's code behind file. In C# it is this extension (.aspx.cs). Implementing this (or passing it to a separate class file) is a simple as passing it to the class via a method parameter or class property, etc.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900