Currently, I'm saving user data via cache, which is probably not the best thing to do since the server will eventually reset itself and clear the data. So, if my understanding is correct, I was thinking of saving user's personal data on the client side if
possible. My question is how do I do that and if there's any reason not to?
The data is just items they've flagged to look at. It's partly me being lazy, not wanting to store that data on the server, and I hate dealing with XML unless asp.net has their own parser already made for me.
If i consider storing this in the user's profile kept in database (will be used after log in), or in cookie (don't require login to be used). This will keep this setting forever - your users will appreciate that :)
oh, basically shopping cart stuff? Then sure....cookie will be a great option.
A list of item numbers yeah. Though now I'm not sure how to store that data into the cookie. Was just reading them up and I wonder, how to go about storing a list of item numbers. Not an actual list variable, cause my stuff is stored in a datatable. I just
need one number from the datatable then I can get the row and add it in.
Found this http://stackoverflow.com/questions/4537386/asp-net-mvc-saving-list-to-cookie
However, I don't know what using statement I need to get access to that SerializeList function. I also don't know how I would read that data back in as a list.
OK, you want to save some items to cookie, and you found a reference about this. It's seems useful to you. The way you found need to serialize the list firstly to save and then deseriablize to retrieve. It's complicated.Tthere are a better mechanism to implement
your requirement:
The elements in Generic List can be accessed using an integer index. And the cookie stores the data with name-value pair and retrieve the data/value with the cookie name. So you can implement your requirement like this:
protected void SaveItemToCookie()
{
List<string> list = new List<string>();
list.Add("a");
list.Add("c");
list.Add("d");
//construct
int i = 1;
foreach (string value in list)
{
string cookieName = "name" + i.ToString();
i++;
Response.Cookies[cookieName].Value = value;
}
}
protected List<string> RetrieveItemFromCookie()
{
List<string> list = new List<string>();
for (int i = 0; i < 3; i++)
{
string cookieName = "name" + i.ToString();
list.Add(Request.Cookies[cookieName].ToString());
}
return list;
}
At last I want to recommend this article about ASP.NET Cookie to you:
mtewks
Member
431 Points
308 Posts
Saving User Data
Feb 23, 2012 12:33 PM|LINK
Currently, I'm saving user data via cache, which is probably not the best thing to do since the server will eventually reset itself and clear the data. So, if my understanding is correct, I was thinking of saving user's personal data on the client side if possible. My question is how do I do that and if there's any reason not to?
Curt_C
All-Star
66014 Points
7639 Posts
Moderator
Re: Saving User Data
Feb 23, 2012 12:38 PM|LINK
just toss it into a cookie if you want to put it on the client...
But if they clear their cookies it's lost.
Why would you not want to use a server side storage system (DB, XML, etc)? This would be the prefferred method over all others.
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
mtewks
Member
431 Points
308 Posts
Re: Saving User Data
Feb 23, 2012 12:42 PM|LINK
The data is just items they've flagged to look at. It's partly me being lazy, not wanting to store that data on the server, and I hate dealing with XML unless asp.net has their own parser already made for me.
Curt_C
All-Star
66014 Points
7639 Posts
Moderator
Re: Saving User Data
Feb 23, 2012 12:44 PM|LINK
oh, basically shopping cart stuff? Then sure....cookie will be a great option.
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
abiruban
All-Star
16002 Points
2731 Posts
Re: Saving User Data
Feb 23, 2012 12:47 PM|LINK
Hi
If i consider storing this in the user's profile kept in database (will be used after log in), or in cookie (don't require login to be used). This will keep this setting forever - your users will appreciate that :)
more detail
http://bytes.com/topic/asp-net/answers/707868-best-practices-user-data-sessions
http://aspnetgoodies.wordpress.com/2007/12/11/retrieving-user-specific-data-with-aspnet-membership-and-session-variable/
Thanks...
***DON'T FORGET TO CLICK “MARK AS ANSWER” ON THE POST IF HELPED YOU.
mtewks
Member
431 Points
308 Posts
Re: Saving User Data
Feb 23, 2012 12:55 PM|LINK
A list of item numbers yeah. Though now I'm not sure how to store that data into the cookie. Was just reading them up and I wonder, how to go about storing a list of item numbers. Not an actual list variable, cause my stuff is stored in a datatable. I just need one number from the datatable then I can get the row and add it in.
roopeshreddy
All-Star
20143 Points
3327 Posts
Re: Saving User Data
Feb 23, 2012 01:10 PM|LINK
Hi,
If you are OK with HTML 5, then you can also consider using localStorage option instead of Cookies!
http://zeollar.cloudapp.net/Session/260
http://stephenwalther.com/blog/archive/2011/01/12/asp-net-and-html5-local-storage.aspx
http://roopeshreddy.wordpress.com/2011/12/25/localstorageavoid-cookies/
Hope it helps u...
Roopesh Reddy C
Roopesh's Space
mtewks
Member
431 Points
308 Posts
Re: Saving User Data
Feb 23, 2012 01:42 PM|LINK
Found this http://stackoverflow.com/questions/4537386/asp-net-mvc-saving-list-to-cookie
However, I don't know what using statement I need to get access to that SerializeList function. I also don't know how I would read that data back in as a list.
Mamba Dai - ...
All-Star
23531 Points
2683 Posts
Microsoft
Re: Saving User Data
Mar 01, 2012 08:49 AM|LINK
Hi,
OK, you want to save some items to cookie, and you found a reference about this. It's seems useful to you. The way you found need to serialize the list firstly to save and then deseriablize to retrieve. It's complicated.Tthere are a better mechanism to implement your requirement:
The elements in Generic List can be accessed using an integer index. And the cookie stores the data with name-value pair and retrieve the data/value with the cookie name. So you can implement your requirement like this:
protected void SaveItemToCookie() { List<string> list = new List<string>(); list.Add("a"); list.Add("c"); list.Add("d"); //construct int i = 1; foreach (string value in list) { string cookieName = "name" + i.ToString(); i++; Response.Cookies[cookieName].Value = value; } } protected List<string> RetrieveItemFromCookie() { List<string> list = new List<string>(); for (int i = 0; i < 3; i++) { string cookieName = "name" + i.ToString(); list.Add(Request.Cookies[cookieName].ToString()); } return list; }At last I want to recommend this article about ASP.NET Cookie to you:
http://msdn.microsoft.com/en-us/library/ms178194.aspx
Feedback to us
Develop and promote your apps in Windows Store