How to Save Retrieve Dataset in ViewState | How to retain dataset values on postback

By
Advertisement

Introduction:
In my previous article I have explained about the code used to encrypt and decrypt password. In this article I am going to explain how to store and retrieve dataset values in viewstate.

Explanation:
 
Normally Dataset values are not available on the postback. To make it available on the post back we are storing it in viewstate. 
Below code explains how to store the dataset values in viewstate.
 
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
DataSet ds = new DataSet();
// your code here
sqlDataAdapter1.Fill(ds);
System.IO.StringWriter sw = new System.IO.StringWriter();

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

Below code is used to Retrieve Dataset from View State on PostBack


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

Do you like this article? Help us to improve. If you have any queries post it in comments.

0 comments:

Post a Comment

Online Casino