asp.net serializing

Discussion in 'ASP .Net' started by venky, Jan 12, 2005.

  1. venky

    venky Guest

    I am new to asp.net and web programming. A question regarding the
    state management.


    I query a database and store a record in the class object. Now i want
    to store this object on postbacks. whats the best way?
    If anyone can throw more light on serilzing, it would be great help
    venky, Jan 12, 2005
    #1
    1. Advertising

  2. venky

    Shimon Sim Guest

    Check Session object of the Page class.
    It should be a good place to start.
    Shimon
    www.siatadev.com

    "venky" <> wrote in message
    news:...
    >I am new to asp.net and web programming. A question regarding the
    > state management.
    >
    >
    > I query a database and store a record in the class object. Now i want
    > to store this object on postbacks. whats the best way?
    > If anyone can throw more light on serilzing, it would be great help
    >
    Shimon Sim, Jan 12, 2005
    #2
    1. Advertising

  3. venky wrote:
    > I query a database and store a record in the class object. Now i want
    > to store this object on postbacks. whats the best way?

    The easiest way to persist an object between postbacks is to store it in
    the the Session object. You have three different storage locations for
    the session data:
    1. InProc - session kept as live objects in web server (aspnet_wp.exe).
    This is the default setting.
    2. StateServer - session serialized and stored in memory in a separate
    process (aspnet_state.exe). State Server can run on another machine
    3. SQLServer - session serialized and stored in SQL server

    The storeage location is configured in the web.config file.

    You can also store objects in the page view state. When you to this the
    object(s) are serialized, base64 encoded and included as a hidden text
    field in the HTML output. This field is posted back to the server where
    the object(s) are rehydrated.

    > If anyone can throw more light on serilzing, it would be great help

    You can serialize an object with the classes in
    System.Runtime.Serialization or System.Xml.Serialization. To serialize
    an object (myObject) with System.Xml.Serialization.BinaryFormatter, you
    can do the following (C#):

    MyClass myObject=new MyClass();
    MemoryStream stream=new MemoryStream();
    BinaryFormatter formatter=new BinaryFormatter();
    formatter.Serialize(stream, myObject);

    To deserialize the stream to an MyClass instance, you can do this (C#):
    MyClasss myDeserializedObject=(MyClass) formatter.Deserialize(stream);

    To serialize an object with the System.Xml.Serialization.XmlSerializer
    class do this:

    MyClass myObject=new MyClass();
    XmlSerializer serializer=new XmlSerializer(typeof (MyClass));
    MemoryStream stream=new MemoryStream();
    XmlTextWriter writer=new XmlTextWriter(stream, Encoding.Unicode);
    serializer.Serialize(writer, myObject);

    To deserialize the stream to a MyClass instance, do this:
    MyClasss myDeserializedObject=(MyClass) serializer.Deserialize(stream);

    Anders Norås
    http://dotnetjunkies.com/weblog/anoras/
    =?ISO-8859-1?Q?=22Anders_Nor=E5s_=5BMCAD=5D=22?=, Jan 12, 2005
    #3
  4. Thanks for replying.

    If i decide to use session object, do i have to handle session timeout?
    What if i don't want to have timeout and even if the user keeps his
    browser open and comes next day and should be able to continue from
    where u left previously?
    how do i handle this?

    venky

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    venkat chellam, Jan 13, 2005
    #4
  5. venkat chellam wrote:
    > If i decide to use session object, do i have to handle session timeout?
    > What if i don't want to have timeout and even if the user keeps his
    > browser open and comes next day and should be able to continue from
    > where u left previously?
    > how do i handle this?

    The session configuration element in web.config has a timeout attribute
    which controls how long a session is concidered valid. The session
    timeout is a sliding value; on each request the timeout period is set to
    the current time plus the timeout value.
    If you need the session to be valid "forever", like session at
    Amazon.com or similar, you should use SQL Server Mode.

    For good coverage of session state in ASP.NET read Rob Howards MSDN
    article on the topic
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp12282000.asp

    Anders Norås
    http://dotnetjunkies.com/weblog/anoras/
    =?ISO-8859-1?Q?=22Anders_Nor=E5s_=5BMCAD=5D=22?=, Jan 13, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aidan Glendye

    Serializing classes derived from dataset

    Aidan Glendye, Jul 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    488
    Aidan Glendye
    Jul 28, 2003
  2. Jason Shohet
    Replies:
    4
    Views:
    351
    bruce barker
    Jul 29, 2003
  3. MattB

    problem de-serializing a dataset

    MattB, Apr 27, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    359
    MattB
    Apr 27, 2004
  4. Russ
    Replies:
    1
    Views:
    341
    Peter Morris
    Aug 10, 2004
  5. Max
    Replies:
    47
    Views:
    439
    Michael D. Kersey
    Aug 12, 2006
Loading...

Share This Page