Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i run the code and following error occur

Object reference not set to an instance of an object.

i have a master page and place label and button in which when user login the show his name and logout button

in master page page load code is

<pre lang="c#">public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Context.User.Identity.IsAuthenticated)
            {

                WELCOME.Text = Session["Login2"].ToString();
                WELCOME.Text = Session["Login4"].ToString();

            }
            else
            {

                Session["Login2"] = null;
            }

        }

        protected void LGOT_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Session.Clear();
            Response.Redirect("Login.aspx");


        }
    }
Posted

1 solution

Two things spring to mind:
1) You are setting the same string to two different values in successive lines of code:
C#
WELCOME.Text = Session["Login2"].ToString();
WELCOME.Text = Session["Login4"].ToString();
The first will have no affect on the string as it's value will be overwritten.

2) The same two lines are the ones causing your problem.
If the value "Login2" (or "Login4") is not found in the session variables, then the returned value will be null - at which point the attempt to call the ToString method will fail with an "Object reference not set to an instance of an object." error.
Check them before you use them:
C#
object login2 = Session["Login2"];
object login4 = Session["login4"];
if (Context.User.Identity.IsAuthenticated && login2 != null && login4 != null)
   {
   WELCOME.Text = login4.ToString();
   }
 
Share this answer
 
Comments
vinodkumarnie 6-Apr-13 13:51pm    
Well said sir...
ariesareej 7-Apr-13 4:20am    
for login2 we create another or i write in this
object login2 = Session["Login2"];
object login4 = Session["login4"];
if (Context.User.Identity.IsAuthenticated && login2 != null && login4 != null)
{
WELCOME.Text = login4.ToString();
WELCOME.Text = login2.ToString();
}
OriginalGriff 7-Apr-13 4:37am    
As I said: the second line will be used, the first will have no effect (unless you have created your own class with a Text property and are doing something very, very nasty with it - if so, don't do that via a property, use a method instead so it is obvious you are adding data rather than overwriting it).
It's just like saying:
int i;
i = 6;
i = 8;
The value of "i" after this code will always be "8", the lines setting "6" will always be ignored.

If you want to put both values into the Text property, then you need to add them in:
string str;
str = "hello";
str = str + "world";
Or
string str;
str = "hello";
str += "world";
Or in your case:
WELCOME.Text = string.Format("{0} {1}", login4, login2);
to separate the two items.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900