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.
         
               
JaimeJegoniaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

123654789987Commented:
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
0
JaimeJegoniaAuthor Commented:


Ok, How about 1 & 3?
0
SmithJWCommented:
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.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

123654789987Commented:
Hi,

Actually Session.Add is not necessary

Session["xrefmr"]=this.RefMRNo.SelectedValue;  will create a new session "xrefmr".

So both add and update a session can be done using the same statement
0
JaimeJegoniaAuthor Commented:
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?

 


0
SmithJWCommented:
yes you are correct.

If it exists it wil be updated with 'this.RefMRNo.SelectedValue;' as the value
if it doesnt exists, it will be created with 'this.RefMRNo.SelectedValue' as the value

the session variable is unique per user connection.
multiple users will not be accessing each others variables.
0
MogalManicCommented:
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)
0
MogalManicCommented:
sorry, hit the wrong key.

Instead of setting the key value to null, the easier method would be to remove the key if the value is null.  Somthing like this:

if (this.RefMRNo.SelectedValue!=null)
  Session["xrefmr"]=this.RefMRNo.SelectedValue;  
else
  Session.Remove("xrefmr");

Then code your logic to test for session!=null
  if (Session["xrefmr"]==null)
     //Process no valid choice made
  else
    //process stored selection
0
AGBrownCommented:
SmithJW said: "the session variable is unique per user connection."

This is correct, but is worth expanding on: the session is defined by the cookie sent to the user. It has a unique ID. You can therefore have one person with multiple windows open accessing the same session (log in to your application and in IE go to File, New Window, this will create a new windows with the same session cookie). Without some tricky management, it should therefore not be used to store the "state" of a page, as you may have many disconnected instances of the same page "default.aspx" which need to store different items against the same session variable.

There are many ways of persisting state in .NET, and all have different uses. The main ones are ApplicationState, SessionState, ViewState, Cache, HttpContext.Current.Items, and cookies. This article claims there are nine different options, all of which should be used in different situations:
http://msdn.microsoft.com/msdnmag/issues/03/04/aspnetuserstate/default.aspx

All of the state bags such as ApplicationState, SessionState, ViewState and Cache have problems with null values, as a null value is perceived to be a valid "value". Some bags offer an "exists" method, but even without that there are ways around it such as MogalManic's enumeration through the keys or storing your own ArrayList of the keys you currently have in the state bag, and using something like

  if (ArrayList(Session["myKeyList"]).Contains("myKey") && Session["myKey"] == null)
  { // session does contain myKey, but it has null against it
  }

Andy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JaimeJegoniaAuthor Commented:
Thank you guys for the rich information you have provided me. Now, I have difficulty given splitting points.
 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.