Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I read session variables

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?

like image 311
Marty Goetz Avatar asked Dec 28 '25 16:12

Marty Goetz


1 Answers

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.

like image 177
Karthik Avatar answered Dec 30 '25 06:12

Karthik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!