string user=Session[ "user "].ToString();
if(user== " ")
{
Response.Write( "欢迎 "+user);
}
else
{
Response.Write( "您还没有登陆 ");
}
当user为空时就会报错了,根本就不会Response.Write( "您还没有登陆 ");
我就只好用了try...catch
try
{
string user=Session[ "user "].ToString();
if(user!= " ")
{
Response.Write( "欢迎 "+user);
}
}
catch{
Response.Write( "您还没有登陆 ");
}
请问还有没有什么别的解决办法?
------解决方案--------------------------------------------------------
if(Session[ "user "] ! = null)
{
Response.Write( "欢迎 "+Session[ "user "].ToString());
}
else
{
Response.Write( "您还没有登陆 ");
}
------解决方案--------------------------------------------------------
if(Session[ "user "] ! = null)
{
Response.Write( "欢迎 "+Session[ "user "].ToString());
}
else
{
Response.Write( "您还没有登陆 ");
}
------解决方案--------------------------------------------------------
因为为空时就是没有对象可以实例.所以建议楼主先判断一下Session[ "user "]的值..
if (Session[ "user "] == null)
{
Response.Write( "您还没有登陆 ");
}
else
{
string user=Session[ "user "].ToString();
Response.Write( "欢迎 "+user);
}
------解决方案--------------------------------------------------------
楼上都是正确答案。。
因为为空时就是没有对象可以实例.所以建议楼主先判断一下Session[ "user "]的值..
if (Session[ "user "] == null)
{
Response.Write( "您还没有登陆 ");
}
else
{
string user=Session[ "user "].ToString();
Response.Write( "欢迎 "+user);
}