Skip to content


Saving and Retrieving a Dataset to and from View State.

To save:

sqlDataAdapter1.Fill(dSet);
System.IO.StringWriter sw = new System.IO.StringWriter();

// Write the DataSet to the ViewState property.
dSet.WriteXml(sw);
ViewState["dSet"] = sw.ToString();

To retrieve:

if (Page.IsPostBack)
{
System.IO.StringReader sr =
new System.IO.StringReader((string)(ViewState["dSet"]));
dSet.ReadXml(sr);
}

Without saving and retrieving from and to viewstate in asp.net, using dataset becomes inefficient completely. I’ve been seeked these methods to load information database only once if not already loaded and finally got it.

Posted in Backend Tech.

3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. ivy said

    hi,

    i’m have some problem with this system.io.reader,

    can u help me with the code?

    thank you.

    note::

    i always getting value cannot be null. parameter name s when executing the above statement..

  2. Shinhee Kim said

    Have you tried to convert DataSet’s xml to string and store it to ViewState? For me, it works fine.

  3. Manish said

    thanx a lot

    this is what i was looking for

    thanx again