get session ID in ASP.Net

To get the session id, do this:

// In a user control or page
string sessionId = this.Session.SessionID; 

// In a normal class, running in a asp.net app.
string sessionId = System.Web.HttpContext.Current.Session.SessionID; 

You should not need to:

  • Make any data table or loop anything
  • Use SQL server for session state
  • Handle Session_Start or Session_End

In a cookieless scenario, the session id is created when you access the Session object for the first time. This shouldn’t matter much, because the moment you access the SessionID property, the session object is accessed.

For more info, look into this:

http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.sessionid.aspx

Note: The msdn examples have been written by monkeys.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)