Delete Value from Cookie
-
Hello All, I want to delete value from cookie For Example: The Cookie name [myCooke] The Cookie Contains-------------------------------------------------------------------------------- myCookeLotID1=7amp;LotNo1=003462&LotName1=Lot 7&BuyPrice1=850&StockQuantity1=56 amp;amp;LotID2=5&LotNo2=003859&LotName2=Lot 3&BuyPrice2=154&StockQuantity2=2 localhost/1536149997862429993731246977484829981862* --------------------------------------------------------------------------------------------------- The question: How i can delete [LotName2=Lot 3] value from this cookie? please reply me quickly. Thank you Best Regards Abdullah S. Abdelhay
-
Hello All, I want to delete value from cookie For Example: The Cookie name [myCooke] The Cookie Contains-------------------------------------------------------------------------------- myCookeLotID1=7amp;LotNo1=003462&LotName1=Lot 7&BuyPrice1=850&StockQuantity1=56 amp;amp;LotID2=5&LotNo2=003859&LotName2=Lot 3&BuyPrice2=154&StockQuantity2=2 localhost/1536149997862429993731246977484829981862* --------------------------------------------------------------------------------------------------- The question: How i can delete [LotName2=Lot 3] value from this cookie? please reply me quickly. Thank you Best Regards Abdullah S. Abdelhay
I am going to assume that the cookie contents arn't exactly as you typed it...because it seems there are a few typos in it. The following should work:
string cookie = "LotID1=7&LotNo1=003462&LotName1=Lot 7&BuyPrice1=850&StockQuantity1=56&LotID2=5&LotNo2=003859&LotName2=Lot 3&BuyPrice2=154&StockQuantity2=2";
string[] values = cookie.Split('&');
string[] newValues = (from cv in values
let eqIdx = cv.IndexOf('=')
let name = cv.Substring(0, eqIdx)
let value = cv.Substring(eqIdx + 1)
where name != "LotName2"
select cv).ToArray();string newCookie = String.Join("&", newValues);
-
I am going to assume that the cookie contents arn't exactly as you typed it...because it seems there are a few typos in it. The following should work:
string cookie = "LotID1=7&LotNo1=003462&LotName1=Lot 7&BuyPrice1=850&StockQuantity1=56&LotID2=5&LotNo2=003859&LotName2=Lot 3&BuyPrice2=154&StockQuantity2=2";
string[] values = cookie.Split('&');
string[] newValues = (from cv in values
let eqIdx = cv.IndexOf('=')
let name = cv.Substring(0, eqIdx)
let value = cv.Substring(eqIdx + 1)
where name != "LotName2"
select cv).ToArray();string newCookie = String.Join("&", newValues);
in Fact you cannot delete value from a cookie but you should have to recreate the cookie with the new value as explained by the previous post.
Rama Charan Prasad "Be happy and Keep smiling.Thats what u want be always..:)"
-
Hello All, I want to delete value from cookie For Example: The Cookie name [myCooke] The Cookie Contains-------------------------------------------------------------------------------- myCookeLotID1=7amp;LotNo1=003462&LotName1=Lot 7&BuyPrice1=850&StockQuantity1=56 amp;amp;LotID2=5&LotNo2=003859&LotName2=Lot 3&BuyPrice2=154&StockQuantity2=2 localhost/1536149997862429993731246977484829981862* --------------------------------------------------------------------------------------------------- The question: How i can delete [LotName2=Lot 3] value from this cookie? please reply me quickly. Thank you Best Regards Abdullah S. Abdelhay
We cannot delete a cookie actually. But one thing we can do is like, we can set the expiry date of the cookie to prior date, so that the browser will remove the cookie when it access the cookie for the next time, since it is expired. Expiring the Cookie in Server Side:
if (Request.Cookies["MyCookie"] != null)
{
HttpCookie myCookie = new HttpCookie("MyCookie");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}<u>Expiring the Cookie in ClientSide Side:</u>
<script type="text/javascript">
<!--
deleteCookie('MyCookie');
// -->
</script>Please reply on any doubts. Hope this will be usefull for you. Thanks, Rajdev KR