Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
The code is given below

 public ActionResult Index()
        {
           
            string a = Session["userStatus"].ToString(); //Here the error occured
            if (a!=null)
            { 
            if (Session["userStatus"].ToString() == "2")
            {

                return Redirect("/EditUserProfile");
            }
            }
        
            return View("ActsRules");
        }
Posted

in session userStatus not existing
check the key ward properly
its case sensitive
 
Share this answer
 
Do a null check for the session prior to use it.
Try this-
C#
if (Session["userStatus"]!=null)
{
  if (Session["userStatus"].ToString() == "2")
  {
     return Redirect("/EditUserProfile");
  }
}


Hope, it helps :)
 
Share this answer
 
Comments
Arasappan 24-Nov-15 1:11am    
This one also the same problem appear
Suvendu Shekhar Giri 24-Nov-15 1:49am    
Couldn't understand what are you saying?
Arasappan 24-Nov-15 1:56am    
U say


if (Session["userStatus"]!=null)
{

Where and when you get the value of Session["userStatus"],Then how it should be enter on the loop
Suvendu Shekhar Giri 24-Nov-15 2:04am    
It must be set somewhere else in the application and definitely not in code shared by OP. It doesn't make any sense to set the session value here and cheking in the immediate next line for whether it is null or not.
C#
public ActionResult Index()
       {

           string a = "2";

       Session["userstatus"] = a;
           if (a!=null)
           {
           if (Session["userStatus"].ToString() == "2")
           {

               return Redirect("/EditUserProfile");
           }
           }

           return View("ActsRules");
       }

C#

 
Share this answer
 
v2
Comments
Suvendu Shekhar Giri 24-Nov-15 2:07am    
Let me explain what you are suggesting, line by line-
string a = "2"; --declare a variable and set a value to it
Session["userstatus"] = a; --assign that value to a session variable
if (a!=null) -- chck whether the varibale is null or not

Why? you have set a value to that variable just before 1 line of code, don't you?
Then how you think the value will be changed to NULL?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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