0

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

link1

link2

link 3

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

enter image description here

Community
  • 1
  • 1
Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
  • Reopened the question as the OP is aware of what a `NullReferenceException` is. – ken2k May 26 '14 at 11:55
  • @ken2k So, this awareness avoids duplication? They are still almost same questions. – Soner Gönül May 26 '14 at 11:57
  • @ken2k, Please excuse me , what is OP ?? – Manjuboyz May 26 '14 at 11:58
  • @Manjuboyz [What does OP mean?](http://meta.stackexchange.com/questions/146513/what-does-op-mean) In this case, it is you :) – Soner Gönül May 26 '14 at 11:59
  • @Soner, I don't mind removing the question since even i know, there are many people facing same kind of issue, and I even searched a lot to fix it,even through Stack Overflow, but resolution and reason is not know!!!, So if I am getting the reason, that would be appreciated, Thanks – Manjuboyz May 26 '14 at 12:01
  • As I understand it, this question isn't so much about the NullRef, as it is about how to maintain the LoggedIn user state. – Rik May 26 '14 at 12:02
  • @Rik, I want to know why session or tempdata making it null, if application is idle for few minutes, even though i haven't used any session time!!, I can use my application for hours continuously without any bug or issue, but idle for few mins it won't with stand, willing to share any info to fix this problem, thanks – Manjuboyz May 26 '14 at 12:05
  • 2
    @Manjuboyz Exaclty, the Exception here is a symptom, and not your actual issue, so your title is bad. Research session timeouts in ASP.Net,and if you still can't fix your problem, ask a question about the *essence* of your problem, i.e. session timeouts, instead of the incidental NullRefException. – Rik May 26 '14 at 12:13
  • @Rik, I appreciate your explanation, I even searched and checked even in my both Webconfig files, does any default session timeout is causing the issue, but unfortunately there is nothing related to session timeout, causing this problem, the reason title is give as above, I wanted to know, is anything leftover or wrong syntax i have used in tempdata or session, which making the above error message, thanks – Manjuboyz May 26 '14 at 12:20
  • You are getting the above error message because for whatever reason your session is getting reset after a period of inactivity and you are not checking whether the various session contents are null before you try to access them. You should be doing proper null checks. That should allow you to avoid this error and then you will be left with figuring out _why_ your session is getting reset. – JLRishe May 26 '14 at 12:44
  • @JlRishe, thanks for your reply, i even used null check in action result, like "if (Session["UsrName"] != null) {} " but after few mins it executes the else statement which I have provided, LET ME ASK THIS TO ALL, FORGET SESSION OR TEMPDATA, IS THERE ANY VARIABLE WHICH CAN ABLE TO STORE AND REDIRECT TO ANOTHER PAGE AS LONG AS I NEEDED OR TILL APPLICATION QUITS ?? because I don't think this gonna work when using both session and tempdata !! – Manjuboyz May 26 '14 at 12:49
  • You could use a cookie, as long as it's not any information you must keep secure. – Jeremy A. West May 26 '14 at 12:56
  • Hmmm, I heard of using HttpCookie, but not yet tried, will check that jeremy, try to implement and see it will resolve this, will update the status ,Thanks – Manjuboyz May 26 '14 at 13:00
  • hi jeremy, while browsing yesterday i found one question, just imagine a I have logged in and application works fine,when application is till running, what if user deletes the cookies and refreshes the page once again!!, do you think the data still present in that variable, and I read an article saying it is also not good way of using cookie to transfer the data, what do you suggest ?? or what you all suggest ? Thanks – Manjuboyz May 27 '14 at 05:07

1 Answers1

-1

You can use User.Identity.Name once the user has logged to get the user name. This way you dont have to mess about with temp data, viewbag or viewdata.

KevDevMan
  • 808
  • 11
  • 23