NOTE
it is not duplicate question I am asking, I have read 3 different links from stack as showed below, but none of them shows resolution for my question (or) reason, so please let me know what i am doing wrong
I developed MVC application, where username from login page taken to controller to pull the data, where it loads up the value of that username, so I want that Username to be exist until i quit my application, so what I did was did some R&D what to use to store and save the data and pass it to another page, first I used "TempData" which works fine to my scenario , where it pulls the data as I needed, but the error occurs when application is idle for 5 min then, if i press the Refresh button or F5, I get the above error, username is null generating error message "object reference not set to instance of an object" I know why this error generates. later I searched it again, used session variable to store and send it, even it does works fine same as tempdata but after few mins like 5 mins and more, if page refreshes generates same message, I believe the syntax used in tempdata and session are correct since it works fine, I am posting code of the way I am using it in MVC, please let me know any mistakes I have done, or let me know what should I do to fix this problem, which should work for longer duration of idle, unless I use session time to quit the application.
NOTE
I have not used any session time, like if application is idle for 5 mins it should quit the application.
CODE
public ActionResult ValidateLogIn(FormCollection postedFormData)
{
LoginViewModel.LoginDataModel.UserName = Convert.ToString(postedFormData["UserName"]);
LoginViewModel.LoginDataModel.Password = Convert.ToString(postedFormData["Password"]);
// ViewBag.Message = LoginViewModel.LoginDataModel.UserName;
// TempData["UsrName"] = LoginViewModel.LoginDataModel.UserName;
TempData.Keep();
Session["UsrName"] = LoginViewModel.LoginDataModel.UserName;
// with some code..........
}
public ActionResult LandingPage()
{
//////ViewData["message"] = TempData["UsrName"].ToString();
Session["message"] = Session["UsrName"].ToString();
ViewData["person"] = Convert.ToInt32(TempData["UserTypeID"]);
TempData.Keep();
String PatID = Convert.ToString(Session["message"].ToString());
int PersonType = Convert.ToInt32(ViewData["person"]);
PatientDetailsViewModel = PatientDetailsDataAccessService.LogInEnquiry(PatID);
return View("../PatientDetails/PatientDetailsView", PatientDetailsViewModel.ServiceList.AsEnumerable());
}
and this is the error which generates when refreshed