What is the best way to check for the existence of a session variable in ASP.NET C#?
I like to use String.IsNullOrEmpty()
works for strings and wonder
Checking for nothing/Null is the way to do it.
Dealing with object types is not the way to go. Declare a strict type and try to cast the object to the correct type. (And use cast hint or Convert)
private const string SESSION_VAR = "myString";
string sSession;
if (Session[SESSION_VAR] != null)
{
sSession = (string)Session[SESSION_VAR];
}
else
{
sSession = "set this";
Session[SESSION_VAR] = sSession;
}
Sorry for any syntax violations, I am a daily VB'er