locked
Session values returning null RRS feed

  • Question

  • User-1453200658 posted

    Hello there,

    I am running an ASP MVC site locally (.Net 4.7) and am experincing an issue when trying to retrieve a value from session.

    I am calling the code from a 

            private ActionResult countries()
            {
                string[] tcode = System.Web.HttpContext.Current.Session["tCode"].ToString().Split(',');  //LINE OF ERROR
    
                var personModel = new PersonModel
                {
                    Countries = PopulateDropDown(" SELECT * FROM `dotable` WHERE `tCode` IN ('" + string.Join("','", tcode) + "') " +
                                                 " GROUP BY `tCode` ORDER BY `tCode` ASC;", "tCode", "tCode")
                };
    
                return View(personModel);
            }

    But on return I have this error

    Object reference not set to an instance of an object.
    System.Web.SessionState.HttpSessionState.this[string].get values returning null

    On this line

    string[] tcode = System.Web.HttpContext.Current.Session["tCode"].ToString().Split(',');  //LINE OF ERROR

    Please can you help me ?

    this is controller.cs

           [HttpGet]
            public ActionResult Index()
            {
                sessionuser();
                recognize();
                return countries();
            }
    
            private ActionResult countries()
            {
                string[] tcode = System.Web.HttpContext.Current.Session["tCode"].ToString().Split(',');  //LINE OF ERROR
    
                var personModel = new PersonModel
                {
                    Countries = PopulateDropDown(" SELECT * FROM `dotable` WHERE `tCode` IN ('" + string.Join("','", tcode) + "') " +
                                                 " GROUP BY `tCode` ORDER BY `tCode` ASC;", "tCode", "tCode")
                };
    
                return View(personModel);
            }
    
            private void sessionuser()
            {
                if (!String.IsNullOrEmpty(HttpContext.User.Identity.Name.ToString()))
                {
                    System.Web.HttpContext.Current.Session["tUser"] = HttpContext.User.Identity.Name.ToString();
                }
    
                System.Web.HttpContext.Current.Session.Timeout = 28800;
            }
    
            private void recognize()
            {
                try
                {
                    string cs = ConfigurationManager.ConnectionStrings["cnj"].ConnectionString;
    
                    using (var connection =
                        new MySqlConnection(cs))
                    {
                        string commandText = "SELECT `tUser`,`tCode` FROM `tUser` WHERE `tUser`=@Username;";
    
                        using (var command =
                            new MySqlCommand(commandText, connection))
                        {
                            if (!String.IsNullOrEmpty(HttpContext.User.Identity.Name.ToString()))
                            {
                                command.Parameters.AddWithValue("@Username", HttpContext.User.Identity.Name.ToString());
                            }
    
                            connection.Open();
    
                            string tUser = string.Empty;
                            string tcode = string.Empty;
    
                            using (MySqlDataReader reader =
                                command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    tUser = reader["tUser"].ToString();
    
                                    if (string.IsNullOrEmpty(tCode))
                                    {
                                        tCode = reader["tCode"].ToString();
                                    }
                                    else
                                    {
                                        if (reader["tCode"] != DBNull.Value)
                                        {
                                            tCode = tCode + "," + reader["tCode"].ToString();
                                        }
                                    }
    
                                    System.Web.HttpContext.Current.Session["tUser"] = tUser.ToString();
                                    System.Web.HttpContext.Current.Session["tCode"] = tCode.ToString();
                                    System.Web.HttpContext.Current.Session.Timeout = 28800;
    
                                    if (String.IsNullOrEmpty(tUser))
                                    {
                                        ViewBag.Message = String.Format("No user!");
                                    }
                                }
                            }
                        }
                        connection.Close();
                    }
                }
                catch (Exception ex)
                {
                    TempData["Message"] = "Login failed.Error - " + ex.Message;
                }
            }
    
            [HttpPost]
            public ActionResult Index(PersonModel person)
            {
                string[] tcode = System.Web.HttpContext.Current.Session["tcode"].ToString().Split(',');
    
                person.Countries = PopulateDropDown(" SELECT * FROM `dotable` WHERE `tCode` IN ('" + string.Join("','", tcode) + "') " +
                                                    " GROUP BY `tCode` ORDER BY `tCode` ASC;", "tCode", "tCode")
    
                if (ModelState.IsValid)
                {
                    return View(person);
                }
    
                return View(person);
            }

    Thursday, February 4, 2021 1:20 PM

Answers

  • User-474980206 posted

    You must always test if a value is stored in session before you use it. Maybe there is a code path that uses the session value before setting it, or session was cleared.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 4, 2021 3:27 PM