Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Developers,

I have a cookie named Students in my code,
that stores multiple name of students.

Sample:

HttpCookie mycookie = new HttpCookie("Students");
mycookie.Values.Add("RollNo", obj.RollNo);
mycookie.Values.Add("StudName", obj.StudName);

Now, with this code I can successfully store my values,
retrive them and display them.

Output of this:

mycookie["RollNo"].ToString() = "10,12,14,15"
mycookie["StudName"].ToString() = "abc,def,lmn,xyz"

But now I want to delete the value from this values.

eg. I want to delete Roll number 12 from this string values then related value def should be deleted.

Please tell me is there any way to do this?
Or I should follow the string class to achieve my requirement.

How can I achieve this?

Please help me guys.
Posted

Hi,

I think it will work.But not sure.

if (Request.Cookies[Students ] != null)
{
    string userSettings;
    if (Request.Cookies[Students][RollNo]!= null)
    {
if((Request.Cookies[Students][RollNo])=="12"){
 Request.Cookies.Delete[Students][RollNo]; }
}
 
Share this answer
 
Comments
Member 10359492 30-Oct-14 5:13am    
Thanks for posting, but it'll not work my friend,
It'll give the same result as mycookie["RollNo"].ToString() (Shown in question)
So I cant remove that. I want to delete a particular value not whole set of values.
After searching a lot on web I found the solution
SQL
for (int i = 0; i < mycookie["RollNo"].ToString().Split(',').ToArray().Length; i++)
{
    if (RollNo == mycookie["RollNo"].ToString().Split(',').ToArray()[i])
    {
        mycookie["RollNo"] = String.Join(",", mycookie["RollNo"].ToString().Split(',').ToArray().Where(val => val != RollNo));
        mycookie["StudName"] = String.Join(",", mycookie["StudName"].ToString().Split(',').ToArray().Select((s, index) => new { s, index }).Where(x => x.index != i).Select(x => x.s));
    }
}

Thanks to coaders...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900