Can't detect whether Session variable exists
Do not use ToString() if you want to check if Session["company_path"] is null. As if Session["company_path"] is null then Session["company_path"].ToString() will give you exception.
Change
if (System.Web.HttpContext.Current.Session["company_path"].ToString() != null){ company_path = System.Web.HttpContext.Current.Session["company_path"].ToString();}else{ company_path = "/reflex/SMD";}
To
if (System.Web.HttpContext.Current.Session["company_path"]!= null){ company_path = System.Web.HttpContext.Current.Session["company_path"].ToString();}else{ company_path = "/reflex/SMD";}
This can be solved as a one liner in the latest version of .NET using a null-conditional ?.
and a null-coalesce ??
:
// Check if the "company_path" exists in the Session contextcompany_path = System.Web.HttpContext.Current.Session["company_path"]?.ToString() ?? "/reflex/SMD";
Links:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operatorhttps://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators
If deploying on Azure (as of August 2017), it is useful to also check if the Session keys array is populated, e.g.:
Session.Keys.Count > 0 && Session["company_path"]!= null