title: retrive session varriable and use it to viewstart in MVC 4 (razor)
body: <p>hi I was trying to retrieve my session value and use it to _layout.cshtml, but every time i am getting null exception<code>(Object reference not set to an instance of an object.)</code>  here is my code. what am i doing wrong. layout.cshtml could not load ~/Views/Shared/_LayoutHome.cshtml  layout. beacouse it isn't getting any  session value.</p>
  
  <pre><code>@{
      if(HttpContext.Current.Session["LoggedUserType"].ToString() == "superadmin" &amp;&amp;  HttpContext.Current.Session["DEPTNM"].ToString() == "AlchemyAdmin")
      {
          Layout = "~/Views/Shared/_Layout.cshtml";
      }
      else
      {
          Layout = "~/Views/Shared/_LayoutHome.cshtml";
  
      }    
  }
  </code></pre>
  
  <p>here is my controller code, i coudnt use my session value.</p>
  
  <pre><code>[HttpPost]
          public ActionResult Index(LoginModel model)
          {
  
  
              if (ModelState.IsValid)
              {
                  if (DataAccess.LoginDAL.AdminIsValid(model.LOGINID, model.LOGINPW))
                  {
  
                      var result = (from n in db.AslUsercoDbSet
                          where n.LOGINID == model.LOGINID &amp;&amp;
                                n.LOGINPW == model.LOGINPW
                          select new
                          {
                             companyid= n.COMPID, 
                             userid =  n.USERID, 
                             deptname= n.DEPTNM, 
                             usertype = n.OPTP
                          }
                          );
                      foreach (var n in result)
                      {
                          Session["loggedCompID"] = n.companyid;
                          Session["loggedUserID"] = n.userid;
                          Session["LoggedDepartment"] = n.deptname;
                          Session["LoggedUserType"] = n.usertype;
                      }
  
                      FormsAuthentication.SetAuthCookie(model.LOGINID, true);
                      return RedirectToAction("Index", "Home");
                  }
                  {
                      ModelState.AddModelError("", "Invalid Username or Password");
                  }
              }
              return View();
          }
  </code></pre>
  
  <p>I am adding my dataaccess layer code here . this validate the user if exist.</p>
  
  <pre><code> internal static bool AdminIsValid(string username, string password)
          {
              bool authenticated = false;
  
              string query = string.Format("SELECT * FROM [AslUsercoes] WHERE LOGINID = '{0}' AND LOGINPW = '{1}'", username, password);
  
              SqlCommand cmd = new SqlCommand(query, conn);
              conn.Open();
  
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              DataTable ds = new DataTable();
              da.Fill(ds);
   SqlDataReader sdr = cmd.ExecuteReader();
              authenticated = sdr.HasRows;
              conn.Close();
              return (authenticated);
          }
  </code></pre>