using session variables vs. storing them in a database table, c# asp.net

I had a bit problem with my session variables; thus switched to use a sql server table to store my values between the sessions. I intend to go back and use session variables again, however, I like to learn a bit more about advantages/disadvantages of these two methods.

Question: What are the advantages of session variables over storing the values in a SQL Server table between the sessions? And visa verse.

Could you possibly list the key points for using session variables?

Thank you
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
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.

käµfm³d 👽Commented:
By "session variables" I assume you are referring to in-memory storage (on the web server), because you can also have session stored in the database as well. (By that I am not referring to what I think you are doing, but rather you still do Session["blah"], but it's stored in the database.) In-memory sessions will be orders of magnitude faster than a database because you are dealing with local memory on the web server. With a database you eat the cost traversing the network to get to the database itself. With databases you also have to set up your table structure, whereas with in-memory you just stick a POCO into the session. In-memory, because it is in memory, will consume resources on the web server. If you are storing large things in session, then you don't want to use in-memory because every user that is logged in might have one or more of these large objects in their own session, consuming memory. With a database, this is not as big of a concern, but again, you are sending these large objects back and forth across the network.
0
Dave BaldwinFixer of ProblemsCommented:
Session variables are short term storage while databases entries are 'permanent'.  Session data expires after I think 30 minutes of inactivity while the database entries don't expire at all.
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
kaufmed,

- The values stored are short strings like "2014", "Site 1", etc.

Could you please comment on the following statements?

- Session variable: expires in set number of minutes or when a session ends.
- Example of session available is Session["YYYY"]="2014";

- Application variable expires only when all users quit the application.
- Please give a sample (syntax) of how an application variable is set:

- Application variable shouldn't be used for something like ="2014"; because different users may be working with different years values and that will cause conflict.

-use application variables for commonly shared data pieces all visitors may use.

Sorry for the long list. Respond to as many of them you like.

Regards,

Mike

Edited:
Dave Baldwin,
Thank you for the post. I didn't see your comment prior to my post.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

käµfm³d 👽Commented:
Application variable expires only when all users quit the application.
Not accurate. Application variables will hold their values as long as the application is loaded into memory. How long this is is configured in your IIS setup (though I believe the clock does start running once there has been no activity on the site in X number of minutes).

Please give a sample (syntax) of how an application variable is set:
Use the Application property: http://msdn.microsoft.com/en-us/library/vstudio/94xkskdf(v=vs.100).aspx

Application variable shouldn't be used for something like ="2014"; because different users may be working
Perhaps. If all users need to see the same value, and no one will modify it, then an application variable might make sense. If users can update the data, then you'll need to think it through.

use application variables for commonly shared data pieces all visitors may use
Agreed. An application variable is really more for storing some aspect of the application itself. This may or may not have a visible effect to your users.
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
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.
0
käµfm³d 👽Commented:
Correction to Dave Baldwin's comment:

Session timeout is configurable per application, and the default value is 20 minutes.
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
ASP.NET

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.