检查会话是否为空或不 [英] Checking session if empty or not
本文介绍了检查会话是否为空或不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问:
在检查会话如果为空或not.Should我用
有些事情就像:
如果(会话[emp_num]!= NULL)
{ 如果(!string.IsNullOrEmpty(会话[emp_num]的ToString())。)
{
//将code
}
}
或只是
如果(会话[emp_num]!= NULL)
{ //将code
}
因为有时当我只检查:
如果(!string.IsNullOrEmpty(会话[emp_num]。的ToString()))
{
//将code
}
我面对以下异常:
空引用例外
块引用>解决方案使用这个,如果会话变量emp_num将存储的字符串:
如果(!string.IsNullOrEmpty(会话[emp_num]作为字符串))
{
//将code
}如果它不存储字符串,但一些其他类型的,你应该只访问值,如你的第二个例子之前检查空。
Q:
When checking session if empty or not.Should I use
some thing like that:
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
解决方案Use this if the session variable emp_num will store a string:
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.
这篇关于检查会话是否为空或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文