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.