I’m trying to achieve something like this:
DataTable dt=new DataTable();
if (Session[Request.QueryString["Id"].ToString()] == null)
{
Session[Request.QueryString["Id"].ToString()] = adRepo.GetPostById(Convert.ToInt32(Request.QueryString["Id"]));
}
dt = (DataTable)Session[Request.QueryString["Id"].ToString()];
is it the suggested thing to do?
I know the session count will go on enormously. But is there any alternative for this?
There’s no reason you cannot use a query string variable as a session key but as Simon pointed out this MAY be a candidate for caching.
What are you trying to accomplish with this? From your example it looks like you’re passing around an ID for a post of some sort and then storing the data table that represents that post in session by the post ID. Are you clearing out the session variable? How much data is in the data table? Is this something that other users may want/need to access? What will the user do with the information in the data table? Even if it’s not a candidate for caching, if the user is accessing multiple posts you’re going to have a lot of garbage clogging up your session very quickly if you’re not careful. How heavy is the query? Would SQL server caching be an option here?
A little more information would go a long way towards a good solution.