Ali Khan

Ali Khan

  • NA
  • 11
  • 450

How to retrieve data of logged in user through Session

Jan 28 2018 9:03 AM

I want to retrieve whole row of data from database when a specific user log in. and show it in my view of profile.

this is my Controller already assigned it a Session variable on ID, I now want to retrieve data from database through this U_ID.


  1. public ActionResult Index(Account_Details log)  
  2.     {  
  3.   
  4.         var obj = db.Account_Details.Where(u => u.Name.Equals(log.Name) && u.Password.Equals(log.Password)).FirstOrDefault();  
  5.   
  6.         if (obj != null)  
  7.         {  
  8.   
  9.             Session["loginid"] = obj.U_ID;  
  10.             Session["name"] = obj.Name;  
  11.             return RedirectToAction("Welcome");  
  12.         }  
  13.   
  14.         else  
  15.         {  
  16.             return View();  
  17.         }  
  18.     }  

this is my Model where I've assigned a foreign key to Basic_Detail table in database. So, that I can get Basic_Detail row of data through U_ID
 
  1. public partial class Account_Details  
  2. {  
  3.     public Account_Details()  
  4.     {  
  5.         this.Basic_Details = new HashSet();  
  6.     }  
  7.   
  8.     public int U_ID { getset; }  
  9.     public string Name { getset; }  
  10.     public string Password { getset; }  
  11.     public string Email { getset; }  
  12.     [DataType(DataType.Date)]  
  13.     [MinAge]  
  14.     public string DOB { getset; }  
  15.   
  16.     public virtual ICollection Basic_Details { getset; }  
  17. }  
 

Answers (1)