Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. HttpContext.Current.Session is getting null

HttpContext.Current.Session is getting null

Scheduled Pinned Locked Moved ASP.NET
helpcsharpasp-netdesign
8 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ash04
    wrote on last edited by
    #1

    Hi All, I am using basepage in my ASP.Net code but in base page I am not getting session. My code is as follows: public class DD_AdmBasePage : System.Web.UI.Page { public string LoginUser; public DD_AdmBasePage() { LoginCheck(); } protected void LoginCheck() { if (HttpContext.Current.Session["LoginUser"]!=null) { LoginUser= HttpContext.Current.Session["LoginUser"].ToString(); } else { HttpContext.Current.Response.Redirect("~/login.aspx"); } } } please help me, I am getting error in following line: if (HttpContext.Current.Session["LoginUser"]!=null) error: object reference not set to an Insatance. and when i am checking HttpContext.Current.Session, it is showing that it is null. I cant understand why it is null. Please help me. Thanks in advance

    DAsh04

    N 1 Reply Last reply
    0
    • A ash04

      Hi All, I am using basepage in my ASP.Net code but in base page I am not getting session. My code is as follows: public class DD_AdmBasePage : System.Web.UI.Page { public string LoginUser; public DD_AdmBasePage() { LoginCheck(); } protected void LoginCheck() { if (HttpContext.Current.Session["LoginUser"]!=null) { LoginUser= HttpContext.Current.Session["LoginUser"].ToString(); } else { HttpContext.Current.Response.Redirect("~/login.aspx"); } } } please help me, I am getting error in following line: if (HttpContext.Current.Session["LoginUser"]!=null) error: object reference not set to an Insatance. and when i am checking HttpContext.Current.Session, it is showing that it is null. I cant understand why it is null. Please help me. Thanks in advance

      DAsh04

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      ash04 wrote:

      and when i am checking HttpContext.Current.Session, it is showing that it is null. I cant understand why it is null.

      Because you base page has stopped calling "OnInit" on it's base page which is "System.Web.UI.Page". So always override it

      public class DD_AdmBasePage : System.Web.UI.Page
      {
      public string LoginUser;

       public DD\_AdmBasePage()
       {
           LoginCheck();
       }
      
       protected void LoginCheck()
       {
           if (HttpContext.Current.Session\["LoginUser"\]!=null)
               LoginUser= HttpContext.Current.Session\["LoginUser"\].ToString();
           else
               HttpContext.Current.Response.Redirect("~/login.aspx");
       }
      
       **protected override void OnInit(EventArgs e) {
            base.OnInit(e);
       }**
      

      }

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      A 1 Reply Last reply
      0
      • N N a v a n e e t h

        ash04 wrote:

        and when i am checking HttpContext.Current.Session, it is showing that it is null. I cant understand why it is null.

        Because you base page has stopped calling "OnInit" on it's base page which is "System.Web.UI.Page". So always override it

        public class DD_AdmBasePage : System.Web.UI.Page
        {
        public string LoginUser;

         public DD\_AdmBasePage()
         {
             LoginCheck();
         }
        
         protected void LoginCheck()
         {
             if (HttpContext.Current.Session\["LoginUser"\]!=null)
                 LoginUser= HttpContext.Current.Session\["LoginUser"\].ToString();
             else
                 HttpContext.Current.Response.Redirect("~/login.aspx");
         }
        
         **protected override void OnInit(EventArgs e) {
              base.OnInit(e);
         }**
        

        }

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

        A Offline
        A Offline
        ash04
        wrote on last edited by
        #3

        thanks navaneeth for participating, I tried but it is not solved my problem. When I checked i found : protected override void OnInit(EventArgs e) { base.OnInit(e); } this function is not called.

        DAsh04

        N 1 Reply Last reply
        0
        • A ash04

          thanks navaneeth for participating, I tried but it is not solved my problem. When I checked i found : protected override void OnInit(EventArgs e) { base.OnInit(e); } this function is not called.

          DAsh04

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          ash04 wrote:

          this function is not called.

          Then you are doing something wrong. See the ASP.NET page life cycle[^] and "Init" is called at the beginning. Can you post the whole base class code ?

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

          A 1 Reply Last reply
          0
          • N N a v a n e e t h

            ash04 wrote:

            this function is not called.

            Then you are doing something wrong. See the ASP.NET page life cycle[^] and "Init" is called at the beginning. Can you post the whole base class code ?

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

            A Offline
            A Offline
            ash04
            wrote on last edited by
            #5

            My full base page code namespace DD { using System; using System.Data; using System.Configuration; using System.Web; using System.Web.SessionState; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// Summary description for DWPage /// </summary> public class DD_AdmBasePage : System.Web.UI.Page { private string LoginUser="" ; public DD_AdmBasePage() { LoginCheck(); } protected string PageName { get { return HttpContext.Current.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length-1]; } } public string LoggedInUser { get { return LoginUser; } } protected void CheckLogin() { if (HttpContext.Current.Session["LoginUser"]!=null) { LoginUser= HttpContext.Current.Session["LoginUser"].ToString(); } else { HttpContext.Current.Response.Redirect("~/login.aspx"); } } protected override void OnInit(EventArgs e) { base.OnInit(e); } } }

            DAsh04

            S 1 Reply Last reply
            0
            • A ash04

              My full base page code namespace DD { using System; using System.Data; using System.Configuration; using System.Web; using System.Web.SessionState; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// Summary description for DWPage /// </summary> public class DD_AdmBasePage : System.Web.UI.Page { private string LoginUser="" ; public DD_AdmBasePage() { LoginCheck(); } protected string PageName { get { return HttpContext.Current.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length-1]; } } public string LoggedInUser { get { return LoginUser; } } protected void CheckLogin() { if (HttpContext.Current.Session["LoginUser"]!=null) { LoginUser= HttpContext.Current.Session["LoginUser"].ToString(); } else { HttpContext.Current.Response.Redirect("~/login.aspx"); } } protected override void OnInit(EventArgs e) { base.OnInit(e); } } }

              DAsh04

              S Offline
              S Offline
              Sandeep Akhare
              wrote on last edited by
              #6

              Can you show your derived Page ? Are you using OnInit event in that page?

              Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog

              A 1 Reply Last reply
              0
              • S Sandeep Akhare

                Can you show your derived Page ? Are you using OnInit event in that page?

                Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog

                A Offline
                A Offline
                ash04
                wrote on last edited by
                #7

                yes it is like this: #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { } #endregion

                DAsh04

                S 1 Reply Last reply
                0
                • A ash04

                  yes it is like this: #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { } #endregion

                  DAsh04

                  S Offline
                  S Offline
                  Sandeep Akhare
                  wrote on last edited by
                  #8

                  Seems to be tricky. Can't guess from this also protected override void OnInit(EventArgs e) { base.OnInit(e); } Check this

                  Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups