locked
Storing and Retrieving ViewState from SQL Server RRS feed

  • Question

  • User2128077945 posted

    I want to have a solution where the web page view state can be saved in sqlserver and after session time out I would like to retrieve the viewstate and render on the page.

    When user works on the page in between if Timeout occurs then all the entered data are saved into viewstate and can be saved in sqlserver.

    Thursday, May 7, 2015 5:31 PM

All replies

  • User-84896714 posted

    Hi Prabal_Sarkar3008,

    Thank you for your post.

    According to your post, we could storing viewstate by extend PageStatePersister class.

    We need to establish a relationship between userid and viewstate。

    When user timeout and reconnect to the site, we could search viewstate in database by userid.

    Link below contains the detailed steps.

    http://www.componentworkshop.com/blog/2009/06/27/advanced-net-storing-viewstate-in-a-database

    In addition,you need to pay attention is you could save user's input periodically.

    Hoping my reply could be helpful to you.

    Best Regards,

    Wang Li

    Friday, May 8, 2015 5:32 AM
  • User2128077945 posted

    Dear Li,

    Thanks for your reply.

    I have gone through the link you provided and created page in vs2010 as below:

    <%--aspx file(DemoPagePersister.aspx)--%>

    <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>ASP.NET Persisting the State of Page Example</title> </head> <body> <form id="form1" runat="server"> <div class="demo-container"> <asp:Label ID="Label1" runat="server" style="font-weight: 700; text-decoration: underline;" >Demo For Persisting the State of Page</asp:Label> <br /> <br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <br /> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>Pfizer</asp:ListItem> <asp:ListItem>Novartis</asp:ListItem> <asp:ListItem>Ranbaxy</asp:ListItem> </asp:DropDownList> <br /> <asp:CheckBoxList ID="CheckBoxList1" runat="server"> <asp:ListItem Text="Football">Football</asp:ListItem> <asp:ListItem Text="Cricket">Cricket</asp:ListItem> <asp:ListItem Text="Tennis">Tennis</asp:ListItem> </asp:CheckBoxList> <br /> <br /> <asp:ListBox runat="server" ID="RadListBox1" CheckBoxes="true" SelectionMode="Multiple" Skin="Office2007" > <Items> <asp:ListItem Text="Australia"></asp:ListItem> <asp:ListItem Text="Brazil"></asp:ListItem> <asp:ListItem Text="Canada"></asp:ListItem> <asp:ListItem Text="Chile"></asp:ListItem> <asp:ListItem Text="China"></asp:ListItem> <asp:ListItem Text="Egypt"></asp:ListItem> <asp:ListItem Text="Germany"></asp:ListItem> <asp:ListItem Text="Indonesia"></asp:ListItem> <asp:ListItem Text="Kenya"></asp:ListItem> <asp:ListItem Text="Mexico"></asp:ListItem> <asp:ListItem Text="New Zealand"></asp:ListItem> <asp:ListItem Text="South Africa"></asp:ListItem> <asp:ListItem Text="USA"></asp:ListItem> </Items> </asp:ListBox> <br /> <br /> <div > <asp:Button ID="SaveButton" runat="server" Text="Save state" OnClick="SaveButton_Click" Skin="Office2007" /> <asp:Button ID="ResetButton" runat="server" Text="Reset state" OnClick="ResetButton_Click" Skin="Office2007" /> </div> <br /> <asp:Label ID="label3" runat="server" ForeColor="#CC0000"></asp:Label> </div> </form> </body> </html> //Code-behind file(DemoPagePersister.aspx.cs) using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;

    namespace DemoWebApp { public partial class DemoPagePersister : System.Web.UI.Page { //protected PersistStateHandler objPersist = new BootstrapDemoWebApp.PersistStateHandler(); private PageStatePersister _PageStatePersister; protected void Page_Load(object sender, EventArgs e) { } protected void SaveButton_Click(object sender, EventArgs e) { SaveButton.Enabled = false; }

    protected void ResetButton_Click(object sender, EventArgs e) {

    } //page state persister protected override System.Web.UI.PageStatePersister PageStatePersister { get { if (_PageStatePersister == null) { string guid = "";

    if (Request["__DATABASE_VIEWSTATE"] == null) { Guid g = Guid.NewGuid(); guid = g.ToString(); } else { guid = Request["__DATABASE_VIEWSTATE"].ToString(); }

    _PageStatePersister = new DatabasePageStatePersister(this, guid);

    Literal l = new Literal(); l.Text = "<div><input type=\"hidden\" name=\"__DATABASE_VIEWSTATE\" value=\"" + guid + "\" /></div>"; this.Form.Controls.Add(l); }

    return _PageStatePersister; } } } }

    //class file (DatabasePageStatePersister.cs) using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Data.SqlClient; using System.Data; namespace DemoWebApp { public class DatabasePageStatePersister : PageStatePersister { string _GUID = "";

    public DatabasePageStatePersister(Page p, string GUID) : base(p) { _GUID = GUID; }

    public override void Load() { string ViewStateData = GetViewState(_GUID);

    this.ViewState = this.StateFormatter.Deserialize(ViewStateData); }

    public override void Save() { string ViewStateData = this.StateFormatter.Serialize(this.ViewState);

    StoreViewState(_GUID, ViewStateData); }

    // Database functions ... //private SqlConnection _c = new SqlConnection("SERVER=.;DATABASE=ViewState;UID=ViewStateDemo;PWD=ViewStateDemo;"); private SqlConnection _c=new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["xxx"].ConnectionString); private void StoreViewState(string guid, string data) { _c.Open();

    try { SqlCommand cmd = new SqlCommand("StoreViewState", _c); cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("guid", SqlDbType.Char, 128)); cmd.Parameters["guid"].Value = guid;

    cmd.Parameters.Add(new SqlParameter("ViewStateData", SqlDbType.Text)); cmd.Parameters["ViewStateData"].Value = data;

    cmd.ExecuteNonQuery(); } finally { _c.Close(); } }

    private string GetViewState(string guid) { string Data = "";

    _c.Open();

    try { SqlCommand cmd = new SqlCommand("GetViewState", _c); cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("guid", SqlDbType.Char, 128)); cmd.Parameters["guid"].Value = guid;

    SqlDataReader r = cmd.ExecuteReader();

    if (r.Read()) { Data = r["viewstatedata"].ToString(); } } finally { _c.Close(); }

    return Data; } } }

    Sorry for the format of the above content.

    I want to get the saved details(in textbox,checkboxlist,dropdownlist etc.) ie, control state when again browsing the same page.

    Also note that view state value is blank next time when we are browsing.

    Your suggestion is awaited..

    Thanks & Regards,

    Prabal

     

     

    Friday, May 8, 2015 8:11 AM
  • User-84896714 posted

    Hi Prabal_Sarkar3008,

    There may something wrong in your code, but it's not easy to find it out.

    In addition, you could use another way to solve your issue.

    When a user first request your website, you could generate a guid and write to user's cookie. This allows each requester has its own unique numberIf a session expires, we have to store the data in database which is not submited for user. When the user come to our site next time, we could find out the data according to user's guid.

    Hoping my reply could be helpful to you.
    Best Regards,
    Wang Li

    Tuesday, May 19, 2015 1:37 AM