Using Session variables without casting them to a new object

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

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?
 
Andy B said:
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?

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
 
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(...);
 
What do you mean?


Lloyd Sheen said:
Andy B said:
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?

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 said:
What do you mean?


Lloyd Sheen said:
Andy B said:
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?

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

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 said:
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;

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

sorry

myVariable=(DataSet)Session["Store"]'
 
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.
 
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
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?

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
 
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...


Hans Kesting said:
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 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?

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
 
Back
Top