Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. session

session

Scheduled Pinned Locked Moved ASP.NET
helpquestiondatabase
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arkiboys
    wrote on last edited by
    #1

    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

    P H B T R 5 Replies Last reply
    0
    • A arkiboys

      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

      P Offline
      P Offline
      padmanabhan N
      wrote on last edited by
      #2

      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[^]

      1 Reply Last reply
      0
      • A arkiboys

        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

        H Offline
        H Offline
        Haroon Sarwar
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A arkiboys

          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

          B Offline
          B Offline
          Brij
          wrote on last edited by
          #4

          Also go through the following link for better understanding in Session Exploring Session in ASP.Net

          Cheers!! Brij

          1 Reply Last reply
          0
          • A arkiboys

            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

            T Offline
            T Offline
            tunsten
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • A arkiboys

              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

              R Offline
              R Offline
              Rutvik Dave
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups