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.

  • Trackback are closed
  • Comments (3)
    • ivy
    • March 6th, 2005 4:52am

    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..

    • Shinhee Kim
    • March 7th, 2005 10:25am

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

    • Manish
    • May 3rd, 2005 3:44am

    thanx a lot

    this is what i was looking for

    thanx again

Comment are closed.