Click here to Skip to main content
15,920,956 members
Please Sign up or sign in to vote.
1.30/5 (3 votes)
See more:
i got an error while running the code

on page1
C#
Session["UserAuthentication"] = username;

Response.Redirect("administrator.aspx?uid=" + username);



on page2
C#
protected void Page_Load(object sender, EventArgs e)
{
   string sName = (string)HttpContext.Current.Session["UserAuthentication"];
   if (sName != null)
   {
      Label1.Text = "Hi "+sName;
   }
}
Posted
Updated 28-Nov-11 20:05pm
v4
Comments
D K N T H 29-Nov-11 1:07am    
in what line causes you an error?, please provide complete details
[no name] 29-Nov-11 1:10am    
EDIT: added "pre" tag

The expression Session["UserAuthentication"] can be null. In this case, in next line you are trying to calculate "Hi " + null which throws the exception.

Please, always run offending code under debugger before asking questions. It would make it all clear for you, so asking the question wouldn't be needed.

—SA
 
Share this answer
 
Comments
Abhinav S 29-Nov-11 1:14am    
My 5. Debugging - my point too.
Sergey Alexandrovich Kryukov 29-Nov-11 1:20am    
Thank you, Abhinav. Debugging effort should be a prerequisite to the questions like this one.
--SA
koolprasad2003 29-Nov-11 1:16am    
Yes. Session variable may be NULL. 5
Sergey Alexandrovich Kryukov 29-Nov-11 1:21am    
Thank you, Prasad.
--SA
Uday P.Singh 29-Nov-11 1:19am    
5ed, I don't know why they are reluctant to use the features of rich IDEs
Check first the value of username if it isn't null.

SQL
if(!String.IsNullOrEmpty(username))
{
   Response.Redirect("administrator.aspx?uid=" + username);
}
else
{
   //do not redirect
}


Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard
 
Share this answer
 
v3
Try debugging this code to figure out where you are trying to access a property which is null.
 
Share this answer
 
"Object reference not set to an instance of an object" is a generic error mostly throws by .NET if object is NULL.

as SA said check Session["UserAuthentication"]. can be null.
check code on Page2.
 
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