Checking session if empty or not

后端 未结 6 852
萌比男神i
萌比男神i 2020-12-28 13:39

I want to check that session is null or empty i.e. some thing like this:

if(Session[\"emp_num\"] != null)
{

   if (!string.IsNullOrEmpty(Session[\"emp_num\"         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-28 14:04

    If It is simple Session you can apply NULL Check directly Session["emp_num"] != null

    But if it's a session of a list Item then You need to apply any one of the following option

    Option 1:

    if (((List)(Session["emp_num"])) != null && (List)Session["emp_num"])).Count > 0)
     {
     //Your Logic here
     }
    

    Option 2:

    List val= Session["emp_num"] as List;  //Get the value from Session.
    
    if (val.FirstOrDefault() != null)
     {
     //Your Logic here
     }
    

提交回复
热议问题