Friday, June 22, 2012

Asp.Net Dynamic Session variables


Asp.Net allows the session keys to be used dynamically both for assigning values to the session keys and for retrieving values from the session keys.

The normal syntax to assign values to session variables is
Session["UserID"] = "1";

However we can also assign values dynamically as follows.

string strUserName = "UserName";
Session[strUserName] = "User1";

The values can also be retireved dynamically as follows

Response.Write("UserID: " + Session["UserID"].ToString() + "<br>");
Response.Write("UserName: " + Session[strUserName].ToString());

The Output will be
UserID: 1
UserName: User1

Search Flipkart Products:
Flipkart.com

No comments: