locked
Session values in HttpContext.Current.Session in C# set and get RRS feed

  • Question

  • User1151703306 posted

    Hello everyone, I need your help.

    I have static class called MailContainer in a MasterPage.

    In MailContainer Class, I have defined properties for get /set like below.

    public static class MailContainer
    {
        public static string TheObjectPropertyEmail
        {
            get
            {
                return HttpContext.Current.Session["TheObjectPropertyEmail"].ToString();
            }
            set
            {
                HttpContext.Current.Session["TheObjectPropertyEmail"] = value;
            }
         } 
    }

    When I try to assign an value like below on Default.aspx.cs with MasterPage.

    MailContainer.TheObjectPropertyEmail = reader["Email"].ToString();

    Its throwing the below exception.

    System.NullReferenceException: Object reference not set to an instance of an object.

    on this line :

    return HttpContext.Current.Session["TheObjectPropertyEmail"].ToString();

    How do I fix this ?

    Thank you in advance for any help.

    Saturday, December 7, 2019 3:09 PM

Answers

  • User475983607 posted

    I believe the following standard Session programming pattern is what you are looking for.

    public static class MailContainer
    {
        public static string TheObjectPropertyEmail
        {
            get
            {
                if(HttpContext.Current.Session["TheObjectPropertyEmail"] == null)
                {
                    return string.Empty;
                }
                else
                {
                    return HttpContext.Current.Session["TheObjectPropertyEmail"].ToString();
                }
                    
            }
            set
            {
                HttpContext.Current.Session["TheObjectPropertyEmail"] = value;
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 8, 2019 1:43 PM

All replies

  • User-1780421697 posted

    Session is available in master page, however you have static class to manage session,that should be void in case of web application because web application running only single instance  for all users.

    If you still need static helper then you need its methods that expect HttpContext as parameter or property with parameter constructore.

    For example:

    static class SessionHelper
    {
        public object GetValue(HttpContext context,string key)
        {
           //here you can use context and get value and return it.
        }
    }

    Sunday, December 8, 2019 2:59 AM
  • User1151703306 posted

    Thank you for reply.

    I don't understand.

    I have tried your suggestion but I have error :

        static class SessionHelper
        {
            public object GetValue(HttpContext context, string key)
            {
                get
                {
                    return HttpContext.Current.Session["TheObjectPropertyEmail"].ToString();
                }
                set
                {
                    HttpContext.Current.Session["TheObjectPropertyEmail"] = value;
                }
            }
        }

    And if try print the value of ;

                Response.Write(Mpdue.MailContainer.TheObjectPropertyEmail);
                Response.End();

    The value is correct, not empty.

    Can you help me ?

    Sunday, December 8, 2019 8:33 AM
  • User475983607 posted

    I believe the following standard Session programming pattern is what you are looking for.

    public static class MailContainer
    {
        public static string TheObjectPropertyEmail
        {
            get
            {
                if(HttpContext.Current.Session["TheObjectPropertyEmail"] == null)
                {
                    return string.Empty;
                }
                else
                {
                    return HttpContext.Current.Session["TheObjectPropertyEmail"].ToString();
                }
                    
            }
            set
            {
                HttpContext.Current.Session["TheObjectPropertyEmail"] = value;
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 8, 2019 1:43 PM
  • User-1780421697 posted
    In case of static class,you will going to share one instance of static class will all your users,that is what i was talking about,who ever comes later will override your data in your current approach, either you need to provide non static class to user or you have to pass HttpContext in each method to solve the issue.
    Monday, December 9, 2019 3:05 AM
  • User475983607 posted

    In case of static class,you will going to share one instance of static class will all your users,that is what i was talking about,who ever comes later will override your data in your current approach, either you need to provide non static class to user or you have to pass HttpContext in each method to solve the issue.

    The pattern work because Session is tied to the user.

    Monday, December 9, 2019 11:53 AM
  • User-1780421697 posted

    Agreed, My point was also to get HttpContext of unique user and in this case getter property pulls HttpContext of current user.

    Tuesday, December 10, 2019 4:48 AM