Reputation: 105
In my project i want store recently accessed Id's(like CompanyId) and based on Id's i need to display most recent 5 records on aspx webpage.For this i am using a session variable like below in my session class:
public static string RecentAssetList
{
get
{
return HttpContext.Current.Session["RECENT_ASSET_LIST"].ToString();
}
set
{
HttpContext.Current.Session["RECENT_ASSET_LIST"] = value;
}
}
And from my page storing the values into session like below.
string assetList = "";
assetList += assetId.ToString() + ",";
SessionData.RecentAssetList = assetList;
but it stores only one Id every time,if new record accessed session showing new one only. How can i store multiple values in my session based that values i want to get data from Database and display in my grid.
Upvotes: 2
Views: 4008
Reputation: 6685
You should read the previously stored value first:
string assetList = SessionData.RecentAssetList;
assetList += assetId.ToString() + ",";
SessionData.RecentAssetList = assetList;
But this solution isn't perfect because new IDs will be appended forever. Better parse and interpret first:
string assetList = SessionData.RecentAssetList;
var ids = assetList.Split(',').ToList().Take(10).ToList();
ids.Add(assetId);
ids = ids.Distinct().ToList();
assetList = string.Join(",", ids);
SessionData.RecentAssetList = assetList;
Upvotes: 2
Reputation: 5119
easiest answer would be (you will still have to apply the logic to only take the last 5, see my second example where it already applies the logic:
string assetList = SessionData.RecentAssetList;
assetList += assetId.ToString() + ",";
SessionData.RecentAssetList = assetList;
so before adding a value you retrieve the values already added to session.
You can also change RecentAssetList to be a List<string>
so that you don't have to manually append it, instead just adding to the list.
public static List<string> RecentAssetList
{
get
{
if (HttpContext.Current.Session["RECENT_ASSET_LIST"] == null)
HttpContext.Current.Session["RECENT_ASSET_LIST"] = new List<string>();
return HttpContext.Current.Session["RECENT_ASSET_LIST"].ToString();
}
set
{
HttpContext.Current.Session["RECENT_ASSET_LIST"] = value;
}
}
And then to add:
List<string> assetList = SessionData.RecentAssetList;
assetList.Insert(0, assetId.ToString());
SessionData.RecentAssetList = assetList.Take(5); // otherwise it's not changed in the session, you can write an extension method where you can add it to the list and submit that list to the session at the same time
.Take(5);
is so that you only take up until the last 5 values inserted
Upvotes: 0
Reputation: 172666
I'm not sure I fully understand, but you can store a dictionary in the session:
public static Dictionary<int, int[]> RecentAssetLists
{
get
{
var session = HttpContext.Current.Session;
var dict = session["RECENT_ASSET_LISTS"];
if (dict == null)
{
dict = new Dictionary<int, int[]>();
session["RECENT_ASSET_LISTS"] = dict;
}
return dict;
}
}
Or you can define your own custom object and store that in the session:
[Serializable]
public sealed class RecentAssetList
{
private List<int> assets = new List<int>();
public List<int> RecentAssets
{
get { return this.assets; }
}
}
public static RecentAssetList RecentAssetList
{
get
{
var session = HttpContext.Current.Session;
var assetlist = session["RECENT_ASSET_LIST"];
return (RecentAssetList)assetlist;
}
}
Upvotes: 0