How to create a session variable in ASP.NET
Submitted by shantanu on Sat, 12/19/2009 - 13:38
- Define an ASP.NET web site.
- Create two pages within the site. The first page will contain an ASP.NET Web Form that will post back to itself and then redirect to the second page.
This code pulls the contents from the <asp:textbox> server control named "txtFirstName" and creates a session variable named"sessFirstName" which is set equal to the contents of the server control. In other words, this code creates a session variable that holds the information the user enters on the form page.
- ASP.NET (C#) - Page 1
- ASP.NET (C#) - Page 2
And for Code behind Page write it as
How to retrieve session Id in Asp.Net
Using session variables is very convenient and usually that is all you will need to maintain state from page to page. However, if you want to save the session state variable in a database for some reason to make sure something is visible only while a session is active (for instance if you want discussion posts to appear to the person posting as long as they are in their session but not to anyone else until you as the administrator approves the post). Here is the code to get the current SessionID.
public static string TheSessionId()
HttpSessionState ss = HttpContext.Current.Session;
HttpContext.Current.Session["test"] = "test";Source: techpint.com