Tharake Gunatilake

Tharake Gunatilake

  • NA
  • 53
  • 73.2k

Set a value for a label in the master page.

Mar 14 2013 12:35 AM
hi guys i have a problem,

I creating a web site which has a master page..The log in page is not a content page of the master page..When user logged in it redirects to the home page and it is a content page of the master page..With the home page all the other pages are content pages.
when the user logged it is easy to show the user name in a label which is in the master page.Because it will show in other all pages.

this is the code i wrote in the master page pageload.

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CompanyID"] != null)
        {
                 
             //Label1.Text = "Your text here";

            string namae = (string)Session["user"];
            Label1.Text = namae;
        }
        else
        {
            Response.Redirect("default.aspx");
        }
   }

But it gives an error...

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 27: 
Line 28:             string namae = (string)Session["user"];
Line 29:             Label1.Text = namae;
Line 30:         }
Line 31:         else



At least i cant show a hard coded value through a label in the master page..What can i do???
Please help me..
Thank you.


Answers (6)