Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
HTML
@if (Session["firstname"] != null && Session["lastname"] != null)
{
   <span class="name">@Session["firstname"] @Session["lastname"]</span>
}
else
{
   @Url.Action("Login", "Login")
}



error:
Object reference not set to an instance of an object.
Posted
Updated 24-Oct-14 2:26am
v2
Comments
Laiju k 24-Oct-14 7:56am    
have you debugged the code.
Session should be initialized earlier for use
[no name] 24-Oct-14 12:10pm    
Check if you have initialized the Sessions and since here you have checked for null,then the problem should not come, please check debugging the code Ihope the error is some where else.
Please debug and post back.:)
Thanks

1 solution

Try pre-pending this to session:
C#
@System.Web.HttpContext.Current.Session


If that doesn't do it, the session object itself may just be entirely null, so it can't even check your conditions in the first place. You should first check the session object itself before checking to see if any stored session objects are null.

C#
Session!=null
 
Share this answer
 

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