Click here to Skip to main content
15,911,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (Session["Type"].ToString().Equals("Admin", StringComparison.CurrentCultureIgnoreCase))
           {
               LinkButton_logout.Text = "Logout";
               Label_loggedin.Text = "Welcome " + Session["Alias"].ToString();
           }
           else if (Session["Type"] == null)
           {
               Response.Redirect("Default.aspx");
           }



link button text and label text not changing ...not even redirecting to default page
Posted
Updated 26-Aug-13 22:06pm
v2
Comments
kishore sharma 27-Aug-13 5:06am    
its my suggestion, when you are comparing any two strings always consider case sensitivity,
so check by changing
Session["Type"].ToString().ToUpper().Equals("Admin".ToUpper(),...............

If Session["Type"] is null, then the attempt to call ToString on it will throw a "Null Reference" exception on the first if condition - which will terminate the operation.

Change the order of your tests - always check for null first, never last!
 
Share this answer
 
First you should check against null then go for comparison. The code should look like

C#
string type = Session["Type"] as string;

if (null == type)
{
    Response.Redirect("Default.aspx", true);
}
if (type.Equals("Admin",StringComparison.OrdinalIgnoreCase))
{
    LinkButton_logout.Text = "Logout";
    Label_loggedin.Text = "Welcome " + Convert.ToString(Session["Alias"]);
}
 
Share this answer
 
Try this
C#
if (Session["Type"] == null)
{
    Response.Redirect("Default.aspx");
}
else if(String.Equals(Convert.ToString(Session["Type"]), "Admin",StringComparison.OrdinalIgnoreCase))
{
    LinkButton_logout.Text = "Logout";
    Label_loggedin.Text = "Welcome " + Convert.ToString(Session["Alias"]);
}


Note: Before this code,check that your sessions have proper values to be handled.

Regards..:)
 
Share this answer
 
v6
Comments
OriginalGriff 27-Aug-13 4:02am    
Won't help! :laugh:
If the variable is null, then ToString will throw an error...
Thanks7872 27-Aug-13 4:37am    
That's the reason i made a note at the end of the answer.
Thanks7872 27-Aug-13 4:39am    
Updated.

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