Solved

ViewState vs Session?

Posted on 2008-06-12
4
965 Views
Last Modified: 2012-06-22
Kind of a novice question: if I create an instance of my own class and try to save it into the Session object, it works, but if I try to save it into my ViewState, then I get an error like "class x is not marked as serializable."  For my edification, why is this?

Thanks very much...
0
Comment
Question by:riceman0
4 Comments
 

Author Comment

by:riceman0
ID: 21775699

Oh (sorry to piggy back), and do I have to be careful about how many objects I save to the session?
0
 
LVL 21

Accepted Solution

by:
naspinski earned 250 total points
ID: 21775814
Session is going to be more versatile.  Though you can save other things into viewstate, it is generally used to preserve your control's state (ie: keep the same text in a box, same element in a dropdown selected) between postbacks.

Session State is somewhat unlimited in size (to a degree) but remember it uses memory on your serving machine, so overuse may be detrimental to performance -for this, keep in mind that each user has their own unique session; but this should only be a problem with a huge amount of users, or unusually large session variable storage.
0
 
LVL 16

Assisted Solution

by:GreymanMSC
GreymanMSC earned 250 total points
ID: 21776214
On Serialization.
 
This has to do with how ViewState variables are passed betwixt server and client machines in byte format.  Serialization is a process for saving data onto a file or memory buffer so a clone may be resurected latter, or (as in this case) so it may be transmitted across a network connection bytewise.
 
In ASP.Net, making a class Serializable is an extremely complicated task, involving adding the Serializable attribute to the class and... no, wait... that's it!
 
(Okay, there is a little more involved if you want to play around with advanced features, but the basics are all automated for you.  Sweet!)
'VB Example
<Serializable()> Class Employee
 
// C# Example
[Serializable] 
class Employee

Open in new window

0
 
LVL 5

Expert Comment

by:Qaiser_Mehmood_Mughal
ID: 21776223
if you want to save the class in ViewState, then
Add following attribute in class
[Serializable]
public class SomeWhat
{

}

or implement ISerializable interface in the class to be the class Serializable.
using System.Runtime.Serializable
public class SomeWhat  : ISerializable
{

}

then add this in to viewstate. okey.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

713 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