Link to home
Create AccountLog in
Avatar of JaimeJegonia
JaimeJegoniaFlag for United States of America

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.RefMRNo.SelectedValue;  
       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.
         
               
Avatar of 123654789987
123654789987

We can check if a session exists using

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
Avatar of JaimeJegonia

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.RefMRNo.SelectedValue;  
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
Avatar of 123654789987
123654789987

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
You said:
Session["xrefmr"]=this.RefMRNo.SelectedValue;  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?

 


SOLUTION
Link to home
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)
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Thank you guys for the rich information you have provided me. Now, I have difficulty given splitting points.