检查会话是否为空

时间:2022-03-07 16:54:31

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"].ToString()))
            {
                //The code
            }
}

Or just

要不就

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

       // The code
    }

because sometimes when i check only with:

因为有时我只检查:

       if (!string.IsNullOrEmpty(Session["emp_num"].ToString()))
                {
                    //The code
                }

I face the following exception:

我面临以下例外:

Null Reference exception

空引用异常

4 个解决方案

#1


45  

Use this if the session variable emp_num will store a string:

如果会话变量emp_num将存储字符串,请使用此选项:

 if (!string.IsNullOrEmpty(Session["emp_num"] as string))
 {
                //The code
 }

If it doesn't store a string, but some other type, you should just check for null before accessing the value, as in your second example.

如果它不存储字符串,而是存储其他类型,则应在访问该值之前检查null,如第二个示例所示。

#2


7  

if (HttpContext.Current.Session["emp_num"] != null)
{
     // code if session is not null
}
  • if at all above fails.
  • 如果以上都失败了。

#3


5  

You need to check that Session["emp_num"] is not null before trying to convert it to a string otherwise you will get a null reference exception.

在尝试将其转换为字符串之前,您需要检查Session [“emp_num”]是否为null,否则您将获得空引用异常。

I'd go with your first example - but you could make it slightly more "elegant".

我会用你的第一个例子 - 但你可以让它更“优雅”。

There are a couple of ways, but the ones that springs to mind are:

有几种方法,但想到的是:

if (Session["emp_num"] is string)
{
}

or

要么

if (!string.IsNullOrEmpty(Session["emp_num"] as string))
{
}

This will return null if the variable doesn't exist or isn't a string.

如果变量不存在或不是字符串,则返回null。

#4


2  

You should first check if Session["emp_num"] exists in the session.

您应首先检查会话中是否存在Session [“emp_num”]。

You can ask the session object if its indexer has the emp_num value or use string.IsNullOrEmpty(Session["emp_num"])

您可以询问会话对象其索引器是否具有emp_num值或使用string.IsNullOrEmpty(Session [“emp_num”])

#1


45  

Use this if the session variable emp_num will store a string:

如果会话变量emp_num将存储字符串,请使用此选项:

 if (!string.IsNullOrEmpty(Session["emp_num"] as string))
 {
                //The code
 }

If it doesn't store a string, but some other type, you should just check for null before accessing the value, as in your second example.

如果它不存储字符串,而是存储其他类型,则应在访问该值之前检查null,如第二个示例所示。

#2


7  

if (HttpContext.Current.Session["emp_num"] != null)
{
     // code if session is not null
}
  • if at all above fails.
  • 如果以上都失败了。

#3


5  

You need to check that Session["emp_num"] is not null before trying to convert it to a string otherwise you will get a null reference exception.

在尝试将其转换为字符串之前,您需要检查Session [“emp_num”]是否为null,否则您将获得空引用异常。

I'd go with your first example - but you could make it slightly more "elegant".

我会用你的第一个例子 - 但你可以让它更“优雅”。

There are a couple of ways, but the ones that springs to mind are:

有几种方法,但想到的是:

if (Session["emp_num"] is string)
{
}

or

要么

if (!string.IsNullOrEmpty(Session["emp_num"] as string))
{
}

This will return null if the variable doesn't exist or isn't a string.

如果变量不存在或不是字符串,则返回null。

#4


2  

You should first check if Session["emp_num"] exists in the session.

您应首先检查会话中是否存在Session [“emp_num”]。

You can ask the session object if its indexer has the emp_num value or use string.IsNullOrEmpty(Session["emp_num"])

您可以询问会话对象其索引器是否具有emp_num值或使用string.IsNullOrEmpty(Session [“emp_num”])