Hello there,
I am running an ASP MVC site locally (.Net 4.7) and am experiencing 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 ?
controller.cs
[HttpGet]
public ActionResult Index()
{
sessionuser();
recognize();
if (Convert.ToInt32(System.Web.HttpContext.Current.Session["UserLiv"]) > 0)
return RedirectToAction("About");
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 UO_AUI = 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);
}