Solved

ViewState vs Session?

Posted on 2008-06-12
4
964 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

773 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