Abhishek Sharma

Abhishek Sharma

  • NA
  • 49
  • 2.8k

Session Goes Null When I am Trying Getting Session Value

Jun 11 2019 5:19 AM
Hi, I am Facing an issue with session. I am creating a session like:
 
(
Game = Players;
Session["Game"] = Game;
)
 
But When I am Trying to use This Session in Another Action Mehtod Then I am Getting Null value . i have allready Rnd on this topic but I am find nothing in google
  1. if (Session["Game"] != null)  
  2. {  
  3. test = true;  
  4. }  
  5. PlayGame = (Players)Session["Game"];  
Please give me a suggestion for That How to fix this issue
 
Here is My Code:-
 
1. > Code Where I am Creating Session
  1. public ActionResult EnterGame(Players Players)  
  2. {  
  3. if (!(string.IsNullOrEmpty(Players.FirstPlayer) && string.IsNullOrEmpty(Players.SecondPlayer)))  
  4. {  
  5. Players Game = new Players();  
  6. Players.DefaultPlayer = Players.FirstPlayer;  
  7. Game = Players;  
  8. Session["Game"] = Game;  
  9. bool test;  
  10. if (Session["Game"] != null)  
  11. {  
  12. test = true;  
  13. }  
  14. return View("PlayGame", Game);  
  15. }  
  16. else  
  17. {  
  18. ModelState.AddModelError(string.Empty, "Both Players Name,s Are Required");  
  19. return View("Index", Players);  
  20. }  
  21. }  
###### Code Where I am use Session to initilize The Session Data
  1. public ActionResult PlayGame(int index) {  
  2. Players PlayGame = new Players();  
  3. bool test;  
  4. if (Session["Game"] != null)  
  5. {  
  6. test = true;  
  7. }  
  8. PlayGame = (Players)Session["Game"];  
  9. PlayGame Game = new PlayGame();  
  10. Game.Play(index, PlayGame);  
  11. if (Game.IsWon == true)  
  12. {  
  13. var Result = new { Message = Game.Message, IsWon = Game.IsWon, AssignedValue = Game.AssignedValue };  
  14. return Json(Result, JsonRequestBehavior.AllowGet);  
  15. }  
  16. else  
  17. {  
  18. var Result = new { Message = Game.Message, DefaultPlayer = PlayGame.DefaultPlayer, AssignedValue = Game.AssignedValue };  
  19. return Json(Result, JsonRequestBehavior.AllowGet);  
  20. }  
  21. }

Answers (1)