Reputation: 78
So I'm trying to make a multivalued cookie that keeps track how many times an specific category is visited on the site.
So I was thinking to make a multivalued cookies where a category is added as key and the value is the amount of times visited.
But for some reason it only creates the cookie but doesn't add a key, value pair.
Here is the code:
String categorie = GetCategorie();
if (Request.Cookies["UserInteresse"] == null)
{
UserInteresse = new HttpCookie("UserInteresse");
Response.Cookies["UserInteresse"]["Favoriet"] = "Geen";
}
else
{
UserInteresse = Request.Cookies["UserInteresse"];
}
if (Request.Cookies["UserInteresse"][categorie] == null)
{
Response.Cookies["UserInteresse"][categorie] = "0";
}
else
{
Response.Cookies["UserInteresse"][categorie]=
Convert.ToInt32(Request.Cookies["UserInteresse"][categorie]) + 1.ToString();
}
Response.Cookies.Add(UserInteresse);
So this code checks first if the cookie exists, if not it will create one with a value to keep track which category is visited the most.
And then it will check if a category as visited before, if not it will add it to the list, if it was visited before its number should be incremented with one.
Everthing is written in ASP.NET (c#)
The code does compile, but when I check the cookie or print out the valuelist I get nothing
Upvotes: 0
Views: 140
Reputation: 78
I found the answer myself after a long search. I found other ways to add values a cookies, I rewrote my code and now it works beautifull.
Here is the new code
categorie = GetCategorie();
if (Request.Cookies["UserInteresse"] == null)
{
UserInteresse = new HttpCookie("UserInteresse");
Response.Cookies.Add(UserInteresse);
Response.Cookies["UserInteresse"]["Favoriet"] = "Geen";
}
else
{
UserInteresse = Request.Cookies["UserInteresse"];
}
if (Request.Cookies["UserInteresse"][categorie]==null)
{
UserInteresse.Values.Add(categorie,"0");
}
else
{
int nieuwaantal = Convert.ToInt32(Request.Cookies["UserInteresse"][categorie]) + 1;
UserInteresse.Values.Remove(categorie);
UserInteresse.Values.Add(categorie, nieuwaantal.ToString());
if (Convert.ToInt32(Request.Cookies["UserInteresse"][categorie]) >= 7 &&
(Request.Cookies["UserInteresse"]["Favoriet"].Equals("Geen")||Convert.ToInt32(Request.Cookies["UserInteresse"][categorie]) >
Convert.ToInt32(Request.Cookies["UserInteresse"][Request.Cookies["UserInteresse"]["Favoriet"]])))
{
UserInteresse.Values.Remove("Favoriet");
UserInteresse.Values.Add("Favoriet", categorie);
}
}
Response.Cookies.Add(UserInteresse);
}
Upvotes: 1