session
-
Hello, Here are the steps I have taken to set and retrieve a session value: 1) In the page load event of the login page, I have placed: Session.Remove("DB"); 2) Then when there is a successful login, in the page load event of the first page that appears i.e. the default.aspx I have placed: Session["DB"] = strDB; //where strDB is a text 3) Then I need to retrieve this session value from within a class file and this is how I am retrieving it in the constructor of the class string strName = (string)System.Web.HttpContext.Current.Session["DB"]; Question: Most of the times there is no problem but sometimes I get an error on step 3) above and it says object reference not set to an instance of an object. Do you see anything wrong please? Thanks
-
Hello, Here are the steps I have taken to set and retrieve a session value: 1) In the page load event of the login page, I have placed: Session.Remove("DB"); 2) Then when there is a successful login, in the page load event of the first page that appears i.e. the default.aspx I have placed: Session["DB"] = strDB; //where strDB is a text 3) Then I need to retrieve this session value from within a class file and this is how I am retrieving it in the constructor of the class string strName = (string)System.Web.HttpContext.Current.Session["DB"]; Question: Most of the times there is no problem but sometimes I get an error on step 3) above and it says object reference not set to an instance of an object. Do you see anything wrong please? Thanks
arkiboys wrote:
reference not set to an instance of an object
are you writing you page load code in, if (!Page.IsPostBack) { //your code here }
Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]
-
Hello, Here are the steps I have taken to set and retrieve a session value: 1) In the page load event of the login page, I have placed: Session.Remove("DB"); 2) Then when there is a successful login, in the page load event of the first page that appears i.e. the default.aspx I have placed: Session["DB"] = strDB; //where strDB is a text 3) Then I need to retrieve this session value from within a class file and this is how I am retrieving it in the constructor of the class string strName = (string)System.Web.HttpContext.Current.Session["DB"]; Question: Most of the times there is no problem but sometimes I get an error on step 3) above and it says object reference not set to an instance of an object. Do you see anything wrong please? Thanks
Can't see anything wrong with the code itself, but this is probably not the best way to be doing this kind of thing. IMO As long as a user is logged in, the Session value should be there, and you should only remove it when they have logged out. - rather than doing Session["DB"] = strDB; on PageLoad of Default.aspx, do it on the login page, when the user has entered their credentials and clicked the login button - rather than doing Session.Remove("DB") in PageLoad of the login page I think you should do Session.Abandon() when the users click on the logout link.
-
Hello, Here are the steps I have taken to set and retrieve a session value: 1) In the page load event of the login page, I have placed: Session.Remove("DB"); 2) Then when there is a successful login, in the page load event of the first page that appears i.e. the default.aspx I have placed: Session["DB"] = strDB; //where strDB is a text 3) Then I need to retrieve this session value from within a class file and this is how I am retrieving it in the constructor of the class string strName = (string)System.Web.HttpContext.Current.Session["DB"]; Question: Most of the times there is no problem but sometimes I get an error on step 3) above and it says object reference not set to an instance of an object. Do you see anything wrong please? Thanks
Also go through the following link for better understanding in Session Exploring Session in ASP.Net
Cheers!! Brij
-
Hello, Here are the steps I have taken to set and retrieve a session value: 1) In the page load event of the login page, I have placed: Session.Remove("DB"); 2) Then when there is a successful login, in the page load event of the first page that appears i.e. the default.aspx I have placed: Session["DB"] = strDB; //where strDB is a text 3) Then I need to retrieve this session value from within a class file and this is how I am retrieving it in the constructor of the class string strName = (string)System.Web.HttpContext.Current.Session["DB"]; Question: Most of the times there is no problem but sometimes I get an error on step 3) above and it says object reference not set to an instance of an object. Do you see anything wrong please? Thanks
The solution to this task is simple. protected void loginButton_Click(object sender, EventArgs e) { // your code for authentication goes here if (myUser.IsAuthenticated()) { Session["username"] = usernameTextBox.Text.Trim(); Response.Redirect("~//Default.aspx"); } else messageLabel.Text = "Authentication failed: Invalid username or password"; } protected void logoutButton_Click(object sender, EventArgs e) { Session.Abandon(); // or Session.Remove("username"); Response.Redirect("~//Logout.aspx"); }
Tunsten
-
Hello, Here are the steps I have taken to set and retrieve a session value: 1) In the page load event of the login page, I have placed: Session.Remove("DB"); 2) Then when there is a successful login, in the page load event of the first page that appears i.e. the default.aspx I have placed: Session["DB"] = strDB; //where strDB is a text 3) Then I need to retrieve this session value from within a class file and this is how I am retrieving it in the constructor of the class string strName = (string)System.Web.HttpContext.Current.Session["DB"]; Question: Most of the times there is no problem but sometimes I get an error on step 3) above and it says object reference not set to an instance of an object. Do you see anything wrong please? Thanks
arkiboys wrote:
Most of the times there is no problem but sometimes I get an error on step 3) above and it says object reference not set to an instance of an object.
This is because your session is expired and the object no longer exists. so everytime when you read a session variable you need to check wether its there or not.
string strName;
if(System.Web.HttpContext.Current.Session["DB"] != null)
strName = (string)System.Web.HttpContext.Current.Session["DB"];
else
Response.Redirect("Your login page");if you open the page and wait until your session expires you will get that error every time (default time of session is 20 min). take a look at the article which 'Brij' has suggested.