Wednesday, March 7, 2012

How to read a set of rows into session variables? C#

Hello ASP.NET C# and SQL gurus

I want to read the results of a set of rows into session variables -- how is it possible?

Let me try explain. I have a query which returns multiple rows, e.g. the following query

SELECT PROFILE_ID, PROFILE_NAME FROM USER_PROFILES returns 5 rows i.e 5 sets of profile_ids and profile_names.

Now, I want to capture these and store them in session variables thus.

Session["PROFILEID_1"] =

Session["PROFILEID_2"] =

Session["PROFILEID_3"] =

Session["PROFILEID_4"] =

Session["PROFILEID_5"] =

Session["PROFILENAME_1"] =

Session["PROFILENAME_2"] =

Session["PROFILENAME_3"] =

Session["PROFILENAME_4"] =

Session["PROFILENAME_5"] =

Thanks in advance!

Fouwaaz

Hi,

Why do you store them in seperate rows. You can store it as datatable and retrieve it when ever you need.

for ex

your select query returns a DataTable dt

then assign it session Session["myTable"] = dt

then when ever you need cast that object to datatable like Session["myTable] as DataTable

Hope it helps

|||

just do a loop of the returned rows.. for example

using (IDataReader rdr = [your returned data read])
{
Session[rdr.GetOrdinal("Profile_ID")] = blah;
Session[rdr.GetOrdinal("Profile_Name")] = blah;

}

Personally, I won't suggest you put so many things in your session object. I'd simply store the entire returned Rows in whatever format when I need to use it.

No comments:

Post a Comment