[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

About Session.Add and Session[""]

Posted on 2006-03-20
10
Medium Priority
?
4,108 Views
Last Modified: 2012-06-27
   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.
         
               
0
Comment
Question by:JaimeJegonia
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 10

Expert Comment

by:123654789987
ID: 16244328
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
 

Author Comment

by:JaimeJegonia
ID: 16244703


Ok, How about 1 & 3?
0
 
LVL 4

Expert Comment

by:SmithJW
ID: 16244770
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Assisted Solution

by:123654789987
123654789987 earned 400 total points
ID: 16244814
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
 

Author Comment

by:JaimeJegonia
ID: 16245100
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
 
LVL 4

Assisted Solution

by:SmithJW
SmithJW earned 800 total points
ID: 16245218
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
 
LVL 21

Expert Comment

by:MogalManic
ID: 16245985
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
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 400 total points
ID: 16246006
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
 
LVL 12

Accepted Solution

by:
AGBrown earned 400 total points
ID: 16248075
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
 

Author Comment

by:JaimeJegonia
ID: 16253452
Thank you guys for the rich information you have provided me. Now, I have difficulty given splitting points.
 
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question