1

We have ASP.Net application. On Normal aspx.cs page I can access session like this,

 Convert.ToString(Session[Resources.AMS.SESSION_USERID]);

But in APP_CODE folder, When i try, I can't get session value, it gives NULL!!

Convert.ToInt32(System.Web.HttpContext.Current.Session[Resources.AMS.SESSION_USERID])

What need to done?

leppie
  • 115,091
  • 17
  • 196
  • 297
Constant Learner
  • 525
  • 4
  • 13
  • 30
  • 1
    The APP_CODE folder is a location on disk. You need to make sure that you are accessing the session from the correct location in code. As in the correct class and method. Where are you accessing it from? – Uatec Jun 07 '13 at 09:07
  • I am accessing this session on one of class file in APP_Code folder. – Constant Learner Jun 07 '13 at 09:09
  • Which class and which method? and when is the method being called? – Uatec Jun 07 '13 at 09:10
  • Check this.. that might be a duplicate question http://stackoverflow.com/questions/621549/how-to-access-session-variables-from-any-class-in-asp-net – Philip Badilla Jun 07 '13 at 09:19
  • There appears to be a fundamental misunderstanding here of code execution order. APP_CODE is not a code site. – Uatec Jun 07 '13 at 09:56
  • Can you post the code for the class that contains this code and tell us where you are using that class from? If you are calling this code in a context where `HttpContext.Current` is valid, for example a web page (and you're just shipping helper methods out to `APP_CODE`) then this should be working, as you would expect. – Sean Airey Jun 07 '13 at 10:05

2 Answers2

1

Only the page will have Web Context values set.

App_Code is where you can store your class files, see here. Class is a template for an object. You only give value to it during run time.

What you can do is add a property to your class.

private int _sessionID;
public int SessionID
{
    get
    {
        return _sessionID;
    }
    set
    {
        sessionID = value;
    }
}

Then in your code behind, (aspx.cs)

var myClass = new myClass();

myClass.SessionID = Convert.ToInt32(System.Web.HttpContext.Current.Session[Resources.AMS.SESSION_USERID])
CurseStacker
  • 1,079
  • 8
  • 19
0

In a class in app_code folder I have this :

public static class SessionTest
{
    public static string OutputSession()
    {
        return System.Web.HttpContext.Current.Session.SessionID.ToString();
    }
}

In a aspx.cs page I have this :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%
    Response.Write(SessionTest.OutputSession());
%>

It outputs the session id. Try this.

Arno 2501
  • 8,921
  • 8
  • 37
  • 55
  • He is already using HttpContext.Current.Session, but it is returning null. This is not a helpful response – Uatec Jun 07 '13 at 09:57