关于会话显示错误混乱对象引用未设置为对象的实例。 [英] About the Session showing the error mess Object reference not set to an instance of an object.

查看:50
本文介绍了关于会话显示错误混乱对象引用未设置为对象的实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,



我的默认页面是Site.master页面&我已经在我的Site.Master中写了代码。下面是:

====================

< pre lang =cs> protected void Page_Load( object sender,EventArgs e)
{

if (!String.IsNullOrEmpty(Session [ userName]。ToString()))
/// / if(Session [UserName]!= null)
{

lblUName.Text = Welcome + Session [ username的]的ToString();

// //lnkLogout.Visible=true;
}
else
{
Response.Redirect( 的Login.aspx);
}

}





============= ==



我也在我的Login.aspx.cs页面中编写代码,对于会话来说......

==== ===========

  if (result >  =  1 
{
会话[ userName] = txtUserName.Text.ToString()。Trim();
Response.Redirect( Site.master);
}
else
lblMsg.Text = 用户名或密码不正确;





========== ====



现在,当我运行程序时,它显示出错误信息:



对象引用没有设置为对象的实例。



请帮帮我哪里是我的错误???

解决方案

尝试If条件,因为.ToString()方法无法处理空值

 if(!String.IsNullOrEmpty(Convert.ToString(Session [userName ])))
{
//你的代码
}


string.IsNullOrEmpty [ ^ ]测试字符串。它检查空字符串引用或空字符串。字符串是引用类型,可以像任何其他引用类型一样等于null。

相当于 result = SomeString == null || SomeString == String.Empty



因为你的代码

Quote:

if(!String.IsNullOrEmpty(Session [userName]。ToString()))



这里你已经转换了Session [userName]使用ToString()函数字符串。在这种情况下,如果会话为null,它将给出NullReferanceException,因为null值不能转换为字符串。



试试这个:

  if (会话[  UserName]!=  null 
{
string s = 欢迎 + Session [ userName]。ToString();
// //lnkLogout.Visible=true;
}
else
{
Response.Redirect( 的Login.aspx);
}







--Amit


amperayani正确使用if(session [username]!= null)在master中。



为什么要从登录页面重定向到母版页?

您应该重定向到.aspx页面。


Sir,

My default page is Site.master page & i have written the code in my Site.Master is bellow:
====================

protected void Page_Load(object sender, EventArgs e)
       {

           if (!String.IsNullOrEmpty(Session["userName"].ToString()))
           ////if (Session["UserName"] != null)
           {

           lblUName.Text = "Welcome" + Session["userName"].ToString();

           //    //lnkLogout.Visible=true;
           }
           else
           {
               Response.Redirect("Login.aspx");
           }

           }



===============

Ans i also write the code in my Login.aspx.cs page is bellow for Session..
===============

if (result >= 1)
           {
               Session["userName"] = txtUserName.Text.ToString().Trim();
               Response.Redirect("Site.master");
           }
           else
               lblMsg.Text = "Incorrect Username or Password";



==============

Now when i run the program it shown the error mess bellow::

Object reference not set to an instance of an object.

please help me where is my mistake???

解决方案

Try the If condition like, because .ToString() method cant handle null value

if (!String.IsNullOrEmpty(Convert.ToString(Session["userName"])))
{
  // your code
}


string.IsNullOrEmpty[^] tests strings. It checks for string references that are null, or empty strings. Strings are reference types and can be equal to null like any other reference type.
It is equivalent to result = SomeString == null || SomeString == String.Empty.

Since your code

Quote:

if (!String.IsNullOrEmpty(Session["userName"].ToString()))


Here you are already converting the Session["userName"] to string using ToString() function. In this case if the session is null it''ll give NullReferanceException, because a null value cannot converted to string.

Try this:

if (Session["UserName"] != null)
{
    string s = "Welcome" + Session["userName"].ToString();
    //    //lnkLogout.Visible=true;
}
else
{
    Response.Redirect("Login.aspx");
}




--Amit


amperayani is correct use if(session["username"]!=null) in master.

Why u r redirecting to master page from login page?
You should redirect to .aspx page.


这篇关于关于会话显示错误混乱对象引用未设置为对象的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆