Aleena Saviour

Aleena Saviour

  • NA
  • 519
  • 87.7k

session and cookies in mvc5

Aug 1 2017 6:22 AM
I have lost data in session/cookies when I call the action of controller second time.what is the reason of this problem?
 
my code is given below...
  1. [HttpPost]  
  2. public ActionResult AddCart(long productId,double Qty,decimal price)  
  3. {  
  4. decimal TotAmount = 0;  
  5. long userId = 0,OrderId=0;  
  6. string status = "incart";  
  7. DataTable dt = new DataTable();  
  8. try  
  9. {  
  10. //var session = HttpContext.Session;  
  11. //if (session["orderid"] != null)  
  12. //{  
  13. // OrderId =Convert.ToInt64(session["orderid"].ToString());  
  14. // TotAmount = Convert.ToDecimal(session["orderAmt"]);  
  15. //}  
  16. HttpCookie myCookie = Request.Cookies["order"];  
  17. if (myCookie != null)  
  18. {  
  19. OrderId =Convert.ToInt64(myCookie.Values["orderid"]);  
  20. //Response.Write("  
  21. " + myCookie.Name + "  
  22.   
  23. " + myCookie.Value);  
  24.   
  25. }  
  26. TotAmount = TotAmount + price;  
  27. SqlCommand cmd = new SqlCommand("OrderSave", con);  
  28. cmd.CommandType = CommandType.StoredProcedure;  
  29. cmd.Parameters.AddWithValue("@PKOderId", OrderId);  
  30. cmd.Parameters.AddWithValue("@PKUserId", userId);  
  31. cmd.Parameters.AddWithValue("@TotAmount", TotAmount);  
  32. cmd.Parameters.AddWithValue("@Status", status);  
  33. cmd.Parameters.AddWithValue("@FKRowStatusId", 1);  
  34. cmd.Parameters.AddWithValue("@TimeStamp", DateTime.Now);  
  35. con.Open();  
  36. SqlDataAdapter da = new SqlDataAdapter(cmd);  
  37. da.Fill(dt);  
  38. con.Close();  
  39. if(dt.Rows.Count>0)  
  40. {  
  41. OrderId = Convert.ToInt64(dt.Rows[0]["PKOrderId"].ToString());  
  42. userId= Convert.ToInt64(dt.Rows[0]["PKUserId"].ToString());  
  43. TotAmount= Convert.ToDecimal(dt.Rows[0]["TotalAmount"].ToString());  
  44. long cartid = 0;  
  45. SqlCommand cmdcart = new SqlCommand("AddCart", con);  
  46. cmdcart.CommandType = CommandType.StoredProcedure;  
  47. cmdcart.Parameters.AddWithValue("@PKCartId", cartid);  
  48. cmdcart.Parameters.AddWithValue("@PKOrderId", OrderId);  
  49. cmdcart.Parameters.AddWithValue("@PKProductId", productId);  
  50. cmdcart.Parameters.AddWithValue("@Quantity", Qty);  
  51. cmdcart.Parameters.AddWithValue("@FKRowStatusId", 1);  
  52. cmdcart.Parameters.AddWithValue("@TimeStamp", DateTime.Now);  
  53. con.Open();  
  54. cartid = Convert.ToInt64(cmdcart.ExecuteScalar());  
  55. con.Close();  
  56. if(cartid>0)  
  57. {  
  58. //session.Add("orderid",OrderId.ToString());  
  59. //session.Add("orderAmt",TotAmount.ToString());  
  60. HttpCookie order = new HttpCookie("order");  
  61. order.Values["orderid"] = OrderId.ToString();  
  62. // Add the cookie.  
  63. Response.Cookies.Add(order);  
  64. status = "success";  
  65. }  
  66. bool sess= Session["orderid"]!=null?true:false;  
  67. }  
  68. }  
  69. catch(Exception ex)  
  70. {  
  71. Console.WriteLine(ex);  
  72. status = "failed";  
  73. }  
  74. return Json(status,JsonRequestBehavior.AllowGet);  
  75. }  
The boolean sess gets value of session...but in next postback the session/cookie returns null. 

Answers (4)