Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8879427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:56:54+00:00 2026-06-14T19:56:54+00:00

I have a class which creates a session the following way: Session[UserId] = UserId;

  • 0

I have a class which creates a session the following way:

Session["UserId"] = UserId; // UserId = 1

In one of the pages on Page_Load, I retrieve the session variable value this way which works fine:

if (Session["UserId"] != null){

    var userid = Session["UserId"];
    Welcome.Text = "Hello, " + userid;

}

Now I need to use the value of the session variable in my classes as well. I used the following method to get the session value int useridsession = Convert.ToInt32(HttpContext.Current.Session["UserId"]); but it always returns null whereas the session is correctly read using Session["UserId"]; in my code behind file.

The mentioned class:

public static DataTable ManageBookingsDataTable()
{

    int useridsession = Convert.ToInt32(HttpContext.Current.Session["UserId"]);
    SqlConnection con = new SqlConnection(Database.ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("select bookings.id,user_id, start_date, end_date, pets.name AS 'Pet name' from bookings AS bookings left join users AS usr ON bookings.user_id=usr.id AND bookings.user_id=1 left join pets AS pets ON pets.id=bookings.pet_id WHERE bookings.user_id=@userid_session", con);
    cmd.Parameters.AddWithValue("@userid_session", useridsession);

    SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    string id = string.Empty;
    string name = string.Empty;
    string startdate = string.Empty;
    string enddate = string.Empty;
    string full_string = string.Empty;


    sqlDa.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            id = dt.Rows[i]["id"].ToString();
            var sdate = dt.Rows[i]["start_date"];
            name = dt.Rows[i]["Pet name"].ToString();
            startdate = dt.Rows[i]["start_date"].ToString();
            enddate = dt.Rows[i]["end_date"].ToString();
            full_string = startdate + " to " + enddate + " (" + name + ")";
            //CurrentBookings.Items.Add(new ListItem(full_string, id));
        }
    }
    return dt;
}

I diagnosed the problem by adding HttpContext.Current.Session["UserId"] = 1; which proves that when I set the session in the same class, the method works.

My question is how do I access the previously created session from any class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:56:55+00:00Added an answer on June 14, 2026 at 7:56 pm

    Session should be available for the session regardless of the class. Why don’t you set and get
    your session values using the same class? All your session stuff in a one place so it is more neat and tidy as well.

    Local.MySession.UserId = UserId;  //Set here
    int myUserId = Local.MySession.UserId; //Get here
    
    //Something like this??
    namespace Local
    {
       //You can add all session variables to this class
       public class MySession
       {
          public static int UserId
          {
             get
             {
                return Convert.ToInt32(HttpContext.Current.Session["userId"] ?? "0");
             }
             set { HttpContext.Current.Session["userId"] = value.ToString(); }
          }
    
          public static string UserEmail //For Example another session variable
          {
             get { return HttpContext.Current.Session["email"] ?? ""; }
             set { HttpContext.Current.Session["email"] = value; }
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class (simulation) which creates an instance of another class (GUI). Inside
i already have a project with TreeNode class which creates a hierachy of nodes
I have a fairly straightforward Java class here which creates 2 thread pools.... Connects
Ok. I have a class Game, which creates an instance of my class Board,
I have the following two classes: TcmTPDataPanel = class(TcmTPBasePanel) Database: TnxDatabase; Session: TnxSession; private
I currently have a custom session handler class which simply builds on php's session
I have a class which would create a bean and the bean has few
I am trying to create downloading progress. I have my class which extends AsyncTask:
I have created a class which extends View class. public class SplashScreen extends View
I have created a class which extends Gallery. There is no onCreate() method in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.