as the situation is described at the bottom(using Webworms not Mvc),
and as i probably need to "master" the implementation of Session variables, almost in every web application , I have constructed a dedicated class that will help me manage it easily.
the approach is as following
this first code block is for usage , you can see the class it is calling on, at the next code block
i am using misspelled names on purpose, to avoid ambiguity(without any prefix), ..and so i could find it much easier on intelliSense that way.
//'importing' the class for current project
using SeSn = Debug_Tests.Seseions.SeSn;
// creating an object (usually with name related to currentProject)
public static SeSn.CreatCurrentSesionVariablsStructNamed CurSesVarStruct = new Seseions.SeSn.CreatCurrentSesionVariablsStructNamed();
// the long name helps me in this little 'chaos'
so far, this is an instance of a struct
, that is 'grouping' or 'tying' all my globals
as one bundle,
so when ever i may need to store my global variables , i will assign the values into the appropriate-struct-variable CurSesVarStruct
has to offer.
and then, all i need is to access session variables once, only to extract the "Variable-collection" -object
, ... as it is actually a session variable, that i keep it's name constant - _CurrentSesionGlobals
i tried my best to describe the background , but in short:
it's the struct that is stored in session as one of the session variables - data type = object
or you could say a clone of the struct to be saved between sessions.
so having that to work with _CurrentSesionGlobals
, I could just access any value i need from session , through it as like in the following :
Assign the struct before storing it in Session
:
CurSesVarStruct.SelectedUercustid = custID;
then the next method - ExtrctSesnVar()
below, allows me to use for example:
Extract a variable that was saved in last session:
custID = ExtractSesnVar().SelectedUercustid;
so SelectedUercustid
is actually one of the struct members.
The Problem
extraction of _CurrentSesionGlobals
out of the session variables.
public static SeSn.CreatCurrentSesionVariablsStructNamed ExtrctSesnVar()
{
var CurrAppGlobals = SeSn.GetValueAS.ACloneOfTheStructObj("_CurrentSesionGlobals");
return (SeSn.CreatCurrentSesionVariablsStructNamed)CurrAppGlobals;
//the question is refereing this location.
}
the question
How could i put a return value for null
result,
or a condition that will first ask if the object
/ a given Session Variable
, that i am trying to extract isn't null
, or does not exist?
as it is now there's an exception error, while i am trying to get the value ... of a non-existing-session-variable.
the next code block .
it's a class that I add into the solution, as a helper to every website application.
it's actually a namespace.
so the class that is responsible to handle session variables is Sesn
namespace Seseions
{
public class Sesn
{
public static bool isNotEmpty()
{
return HttpContext.Current.Session.Keys.Count > 0;
}
public struct CreatCurrentSesionVariablsStructNamed
{
// some of commonly used variables- still testing options..
public int ManagerCustID;
public int SelectedUercustid;
public int recordID;
public int SelectedMonth;
public int SelectedChosenWorker;
public int SelectedYear ;
public string SelectedTable;
public string SelectedColumn;
public string SqlSelectCommandLastQuery;
public string TableOfUsersReference;
public List<string> Fontlist { get; set; }
}
// converts and extract values of session variables
public class GetValueAS
{
public static CreatCurrentSesionVariablsStructNamed ACloneOfTheStructObj(string currntProjectSesVarStructName)
{
if(HttpContext.Current.Session[currntProjectSesVarStructName] != null)
return (CreatCurrentSesionVariablsStructNamed)HttpContext.Current.Session[currntProjectSesVarStructName];
}
public static int _Int(string SesParameterValToReturn)
{
return Convert.ToInt32(HttpContext.Current.Session[SesParameterValToReturn]);
}
public static string _String(string SesParameterValToReturn)
{
return Convert.ToString(HttpContext.Current.Session[SesParameterValToReturn]);
}
public static DataSet _DataSet(string SesParameterValToReturn)
{
return (DataSet)HttpContext.Current.Session[SesParameterValToReturn];
}
public static DataTable _DataTable(string SesParameterValToReturn)
{
return (DataTable)HttpContext.Current.Session[SesParameterValToReturn];
}
public static bool _Bool(string SeSnVarToCheckOn)
{
if (HttpContext.Current.Session[SeSnVarToCheckOn] == null)
return false;
return (bool)HttpContext.Current.Session[SeSnVarToCheckOn];
}
}
// an easy way to access and mange session variables actions
public enum Act
{
Add, Remove, Replace
}
public static void Modify(Act action, string New_SesnVarName= null, object NewP_Value=null, string Currnt_Ses_SesnVarName=null)
{
switch (action)
{
case Act.Remove:
if (isNotEmpty())
HttpContext.Current.Session.Remove(CurSes_ParamName);
break;
case Act.Replace:
HttpContext.Current.Session.Remove(CurSes_ParamName);
HttpContext.Current.Session.Add(New_SesnVarName, NewP_Value);
break;
case Act.Add:
HttpContext.Current.Session.Add(NewQs_SesnVarName, NewP_Value);
break;
}
}
}
}