TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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...
[HttpPost]
public
ActionResult AddCart(
long
productId,
double
Qty,
decimal
price)
{
decimal
TotAmount = 0;
long
userId = 0,OrderId=0;
string
status =
"incart"
;
DataTable dt =
new
DataTable();
try
{
//var session = HttpContext.Session;
//if (session["orderid"] != null)
//{
// OrderId =Convert.ToInt64(session["orderid"].ToString());
// TotAmount = Convert.ToDecimal(session["orderAmt"]);
//}
HttpCookie myCookie = Request.Cookies[
"order"
];
if
(myCookie !=
null
)
{
OrderId =Convert.ToInt64(myCookie.Values[
"orderid"
]);
//Response.Write("
" + myCookie.Name + "
" + myCookie.Value);
}
TotAmount = TotAmount + price;
SqlCommand cmd =
new
SqlCommand(
"OrderSave"
, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(
"@PKOderId"
, OrderId);
cmd.Parameters.AddWithValue(
"@PKUserId"
, userId);
cmd.Parameters.AddWithValue(
"@TotAmount"
, TotAmount);
cmd.Parameters.AddWithValue(
"@Status"
, status);
cmd.Parameters.AddWithValue(
"@FKRowStatusId"
, 1);
cmd.Parameters.AddWithValue(
"@TimeStamp"
, DateTime.Now);
con.Open();
SqlDataAdapter da =
new
SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
if
(dt.Rows.Count>0)
{
OrderId = Convert.ToInt64(dt.Rows[0][
"PKOrderId"
].ToString());
userId= Convert.ToInt64(dt.Rows[0][
"PKUserId"
].ToString());
TotAmount= Convert.ToDecimal(dt.Rows[0][
"TotalAmount"
].ToString());
long
cartid = 0;
SqlCommand cmdcart =
new
SqlCommand(
"AddCart"
, con);
cmdcart.CommandType = CommandType.StoredProcedure;
cmdcart.Parameters.AddWithValue(
"@PKCartId"
, cartid);
cmdcart.Parameters.AddWithValue(
"@PKOrderId"
, OrderId);
cmdcart.Parameters.AddWithValue(
"@PKProductId"
, productId);
cmdcart.Parameters.AddWithValue(
"@Quantity"
, Qty);
cmdcart.Parameters.AddWithValue(
"@FKRowStatusId"
, 1);
cmdcart.Parameters.AddWithValue(
"@TimeStamp"
, DateTime.Now);
con.Open();
cartid = Convert.ToInt64(cmdcart.ExecuteScalar());
con.Close();
if
(cartid>0)
{
//session.Add("orderid",OrderId.ToString());
//session.Add("orderAmt",TotAmount.ToString());
HttpCookie order =
new
HttpCookie(
"order"
);
order.Values[
"orderid"
] = OrderId.ToString();
// Add the cookie.
Response.Cookies.Add(order);
status =
"success"
;
}
bool
sess= Session[
"orderid"
]!=
null
?
true
:
false
;
}
}
catch
(Exception ex)
{
Console.WriteLine(ex);
status =
"failed"
;
}
return
Json(status,JsonRequestBehavior.AllowGet);
}
The boolean sess gets value of session...but in next postback the session/cookie returns null.
Reply
Answers (
4
)
Winform Application controls Displaying top left position
Sorttop 10 Record Based on date in descending order