2

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.

3
  • is assest List a String or a List<T> look at either Session.Add() method or find a way to add the session vars to a List<T> and when the list reaches a count of 5 then perhpas you can do some logic around that.. there are many approaches to this .. when building complex strings try to stay away from operator overloading += for example use a string builder Commented Jan 5, 2012 at 14:00
  • I see some good suggestions below as well just wondering based on the way you named the variable ..can be a bit misleading.. Commented Jan 5, 2012 at 14:03
  • I am getting error : Connot convert string to Sysytem.Collection.generic.List<String> for "TBohnen.jnr " replay how to solve this? Commented Jan 5, 2012 at 17:04

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Getting error here "ids.Add(assetId);" System.Collection.Generic.IEnumarable<string> doesnot contain defination for Add and no extension method Add accepting first argument of type System.Collection.IEnumarable.generic<string>
@IndraRamasani I corrected the code. It should be: var ids = assertList.Split(',').ToList().Take(10).ToList(). Plus you need to add "using System.Linq".
0

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;
    }
}

Comments

0

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

4 Comments

I tryed this but getting "Object reference not set to an instance of an object."Error
HttpContext.Current.Session["RECENT_ASSET_LIST"] = new List<string>(); error:Connot implicitly convert type string to System.Collection.Generic.List<>
Indra what code are you using.. there are alot of examples and suggestions listed here
I am using: 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; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.