JaimeJegonia
asked on
About Session.Add and Session[""]
Dear Experts,
I just to have a clear understanding about the ff:
1.) When I can use the Session.Add ?
Session.Add("xrefmr", this.RefMRNo.SelectedValue );
Session["xrefmr"]=this.Ref MRNo.Selec tedValue;
2.) How do i know if session variable already exist?
3.) What will happen if :
instead of Session["xrefmr"] = null;
I'll make it: Session.Add("xrefmr", null); - to make sure "xrefmr" exist with null value?
Thanks.
I just to have a clear understanding about the ff:
1.) When I can use the Session.Add ?
Session.Add("xrefmr", this.RefMRNo.SelectedValue
Session["xrefmr"]=this.Ref
2.) How do i know if session variable already exist?
3.) What will happen if :
instead of Session["xrefmr"] = null;
I'll make it: Session.Add("xrefmr", null); - to make sure "xrefmr" exist with null value?
Thanks.
ASKER
Ok, How about 1 & 3?
Part 1
Session.Add("xrefmr", this.RefMRNo.SelectedValue );
use this, if you want to create the xrefmr, if it exists, any data will be destroyed
Session["xrefmr"]=this.Ref MRNo.Selec tedValue;
we would use this to check if the session variable exists, or to retreive the information from it, e.g.
Somevar = Session["xrefmr"];
part 2
if (Session["xrefmr"] == null)
{
// if it doesnt exist.. do something here
}
Part 3
your example: Session.Add("xrefmr", null);
would overwrite "xrefmr", no matter wether there was data in it or not. This may be useful sometimes, but mostly id say it's not.
Session.Add("xrefmr", this.RefMRNo.SelectedValue
use this, if you want to create the xrefmr, if it exists, any data will be destroyed
Session["xrefmr"]=this.Ref
we would use this to check if the session variable exists, or to retreive the information from it, e.g.
Somevar = Session["xrefmr"];
part 2
if (Session["xrefmr"] == null)
{
// if it doesnt exist.. do something here
}
Part 3
your example: Session.Add("xrefmr", null);
would overwrite "xrefmr", no matter wether there was data in it or not. This may be useful sometimes, but mostly id say it's not.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
You said:
Session["xrefmr"]=this.Ref MRNo.Selec tedValue; will create a new session "xrefmr".
So, this would mean if "xrefmr" exist it will just update it with a new value if not, it will create it and set value for it? And probably start new session time for it?
BTW: If we are already online, what happened if many of us opening the same page calling the same session variable, are we not going to have problem on this?
More idea please?
Session["xrefmr"]=this.Ref
So, this would mean if "xrefmr" exist it will just update it with a new value if not, it will create it and set value for it? And probably start new session time for it?
BTW: If we are already online, what happened if many of us opening the same page calling the same session variable, are we not going to have problem on this?
More idea please?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
the Session["key"]==null is the quickest and easiest test to see if a key exists and has a value, but if you are creating session values set to NULL, then you will have to enumerate through the keys until it is found:
foreach(string key in Session.Keys)
foreach(string key in Session.Keys)
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you guys for the rich information you have provided me. Now, I have difficulty given splitting points.
if(Session["xff"] == null)
{
//Do some action to fill the Session, with valid data. Either get it from database or from some other source.
}
To clear the session u can do
Session["xff"] = null