I have a c# .net web application. I create session variables but when I try to read them after I leave the page that they were created from I can't.
Created on page 1
Session["UserName"] = "WhatEver";
Then I do
Response.Redirect("~/whatever.aspx");
and try to read to read the session variable in the Page_Load method of the new page
string userName = Session["UserName"].ToString();
I receive Object reference not set to an instance of an object.
Why am I receiving this error and what can I do to fix the problem?
This may help:
Response.Redirect("~/whatever.aspx",false);
From this article:
This does not abort the thread and thus conserve the session token. Actually, this overload is used internally by RedirectFromLoginPage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With