Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / May 2008

Tip: Looking for answers? Try searching our database.

Using Session variables without casting them to a new object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andy B - 23 May 2008 17:02 GMT
I find it a little hard sometimes to cast a Session variable to a different
object, do what I need to do to it and then send it back to the Session. Is
there a way to do this without taking the variable out of the Session first?
I have 15-20 methods on my page codebehind that uses this Session variable,
so I would have to cast/modify/send 15-20 times. Can I do something like
this instead?

(DataSet)Session["Store"].Tables["Contract"].Columns.Add(...);

Or does it actually have to be cast outside of Session in order to work?
Patrice - 23 May 2008 17:17 GMT
Just expose this as you wish behind a wrapper. Basically something such as :

http://weblogs.asp.net/cstewart/archive/2008/01/09/strongly-typed-session-in-asp
-net.aspx


According ot the http://msdn.microsoft.com/en-us/library/aa478952.aspx even
the SQL Session provider serializes at the end of the resuest. So you
shouldn't have to "extract/store" the object to/from the session variable
(anyway if this is behind a wrapper your application won't care care about
this).

--
Patrice

"Andy B" <a_borka@sbcglobal.net> a écrit dans le message de groupe de
discussion : Ok1uS5OvIHA.2292@TK2MSFTNGP05.phx.gbl...
> I find it a little hard sometimes to cast a Session variable to a
> different object, do what I need to do to it and then send it back to the
[quoted text clipped - 6 lines]
>
> Or does it actually have to be cast outside of Session in order to work?
Lloyd Sheen - 23 May 2008 17:22 GMT
>I find it a little hard sometimes to cast a Session variable to a different
>object, do what I need to do to it and then send it back to the Session. Is
[quoted text clipped - 6 lines]
>
> Or does it actually have to be cast outside of Session in order to work?

Why not create a variable.  Ensure variable is correct type.  Init it from
session.  Then you should never have to cast again during the current
execution of the page.

LS
Andy B - 23 May 2008 17:38 GMT
What do you mean?

>>I find it a little hard sometimes to cast a Session variable to a
>>different object, do what I need to do to it and then send it back to the
[quoted text clipped - 12 lines]
>
> LS
Lloyd Sheen - 23 May 2008 17:55 GMT
> What do you mean?
>
[quoted text clipped - 14 lines]
>>
>> LS

Create a page level variable of type DataSet.  Then on load or whenever you
need it.  If you use a property you can do something like:
DataSet myVariable;

In the property get:

if (myVariable ==null)
{
   myVariable=Session["Store"]'
}
   return myVariable;

You then have a varible to use when you require it access it thru the
property and you will either get what has been loaded or will load it and
then present it to you.

LS
Mark Rae [MVP] - 23 May 2008 18:07 GMT
> Create a page level variable of type DataSet.  Then on load or whenever
> you need it.  If you use a property you can do something like:
[quoted text clipped - 7 lines]
> }
>    return myVariable;

Surely that will throw an exception because you're trying to populate a
DataSet variable with an object datatype...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Lloyd Sheen - 23 May 2008 18:44 GMT
>> Create a page level variable of type DataSet.  Then on load or whenever
>> you need it.  If you use a property you can do something like:
[quoted text clipped - 10 lines]
> Surely that will throw an exception because you're trying to populate a
> DataSet variable with an object datatype...

sorry

myVariable=(DataSet)Session["Store"]'
Mark Rae [MVP] - 23 May 2008 19:18 GMT
>>> if (myVariable ==null)
>>> {
[quoted text clipped - 8 lines]
>
> myVariable=(DataSet)Session["Store"]'

Yeah, but that's precisely what the OP is trying to avoid, at least, based
on the thread title...

Not possible, IMO...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

George Ter-Saakov - 23 May 2008 19:29 GMT
Here is how i do staff hope it will help you on your quest to perfect .NET
application :)

1. Create new class clsBrowser for example like this
public class clsBrowser
{
   public int _iUserId;
   public string _sFirstName;
   public string _sLastName;
   public clsShoppingCart _shoppingCart = null;
}

2. Create new class clsStandardPage : System.Web.UI.Page

public class clsStandardPage : System.Web.UI.Page
{
   protected clsBrowserSession _objBs;
   protected override void OnPreInit(EventArgs e)
   {
       Response.CacheControl = "no-cache";
       Response.Expires = -1;
       _objBs = (clsBrowserSession)Session["BrowserSession"];
       if (_objBs == null)
       {
           _objBs = new clsBrowserSession();
           Session["BrowserSession"] = _objBs;
       }
       base.OnInit(e);
   }
}

3. Modify web.config a little.
<pages pageBaseType="clsStandardPage">

........done.....

Now all your pages will be derived from clsStandardPage. All your page's
code will have access to _objBs variable.

no code like that
string sFirstName = (string)Session["FirstName"];

now it's
string sFirstName = _objBs._sFirstName;

Hope you got an idea...
1. It actually reduces a mess if you have a lot of Session variables.
2. No mispells anymore when working with Session object.
3. Code is type safe.
4. Code is faster since everytime you do Session[".."] it's a look up in
hashtable. Plus boxing/unboxint of value types....

George.
Teemu Keiski - 23 May 2008 17:23 GMT
Note that you need to have parentheses to "limit" so that you have the
correct type at hand (note ( - and -  ) around the cast

((DataSet)Session["Store"]).Tables["Contract"].Columns.Add(...);

Signature

Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

>I find it a little hard sometimes to cast a Session variable to a different
>object, do what I need to do to it and then send it back to the Session. Is
[quoted text clipped - 6 lines]
>
> Or does it actually have to be cast outside of Session in order to work?
Hans Kesting - 26 May 2008 10:07 GMT
Andy B has brought this to us :
> I find it a little hard sometimes to cast a Session variable to a different
> object, do what I need to do to it and then send it back to the Session. Is
[quoted text clipped - 6 lines]
>
> Or does it actually have to be cast outside of Session in order to work?

Note: you don't have to "put it back in the session":

If you do

DataSet myDataSet = (Dataset)Sesion["Store"];

then myDataSet will point to the *same* DataSet on the heap as
Session["Store"]. So any changes done through myDataSet will also be
visible to Session["Store"], as they point to the same object.

Hans Kesting
Andy B - 26 May 2008 20:34 GMT
I found out that I can also do something like this:

DataSet DataSet = new DataSet();
Session["DataSet"]=Dataset;

and then later I can do this:

((DataSet)Session["DataSet"]).[whatever you normally do to datasets];

Works just fine and sticks around through Wizard.ActiveStepIndex changes and
page reloads...

> Andy B has brought this to us :
>> I find it a little hard sometimes to cast a Session variable to a
[quoted text clipped - 19 lines]
>
> Hans Kesting

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.