Spread for ASP.NET 13 Product Documentation
Spread for ASP.NET 13 Product Documentation / Developer's Guide / Maintaining State / Saving Data to the Session State
In This Topic
    Saving Data to the Session State
    In This Topic

    Session state management provides server-based state management, where data is saved to separate browser sessions for each user.

    To save the data to the session state, set the IsTrackingViewState property for the active sheet to True, as with saving data to the view state. Then handle the session state in the SaveOrLoadSheetState event.

    Advantages and Disadvantages

    The advantages of using the session state are:

    Using the session state is best if the data is too big to save to the view state.

    The disadvantage of using the session state is the impact on performance. Session state variables stay in server memory until they are either removed or replaced, and therefore can degrade server performance. Session state variables containing blocks of information like large data sets can adversely affect Web server performance as server load increases.

    Using Code

    Use the session state to save data.

    Example

    The following sample illustrates using the session state to save data.

    C#
    Copy Code
    protected void Page_Load(object sender, System.EventArgs e)
    {
    if (this.IsPostBack) return;
    // Connect to NWIND MS Access example with OLE DB.
    OleDbConnection thisConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\\NWIND.MDB");
    // Open connection.
    thisConnection.Open();
    // Create DataSet to contain related data tables, rows, and columns.
    DataSet thisDataSet = new DataSet();
    OleDbDataAdapter orderAdapter = new OleDbDataAdapter("SELECT * FROM Orders", thisConnection);
    orderAdapter.Fill(thisDataSet, "Orders");
    FpSpread1.ActiveSheetView.IsTrackingViewState = true;
    FpSpread1.ActiveSheetView.DataSource = thisDataSet;
    FpSpread1.ActiveSheetView.DataMember = "Orders";
    }
    
    protected void FpSpread1_SaveOrLoadSheetState(object sender, FarPoint.Web.Spread.SheetViewStateEventArgs e)
    {
    if (e.IsSave)
    {
    Session[e.SheetView.SheetName] = e.SheetView.SaveViewState();
    }
    else
    {
    e.SheetView.LoadViewState(Session[e.SheetView.SheetName]);
    }
    e.Handled = true;
    }
    
    VB
    Copy Code
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (Me.IsPostBack) Then Return
        ' Connect to NWIND MS Access example with OLE DB.
         Dim thisConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\NWIND.MDB")
        ' Open connection.
        thisConnection.Open()
        ' Create DataSet to contain related data tables, rows, and columns.
        Dim thisDataSet As New DataSet()
        Dim orderAdapter As New OleDbDataAdapter("SELECT * FROM Orders", thisConnection)
        orderAdapter.Fill(thisDataSet, "Orders")
        FpSpread1.ActiveSheetView.DataSource = thisDataSet
        FpSpread1.ActiveSheetView.DataMember = "Orders"
        FpSpread1.ActiveSheetView.IsTrackingViewState = True
    End Sub
    
    Private Sub FpSpread1_SaveOrLoadSheetState(ByVal sender As Object, ByVal e As FarPoint.Web.Spread.SheetViewStateEventArgs) Handles FpSpread1.SaveOrLoadSheetState
        If (e.IsSave) Then  Session(e.SheetView.SheetName) = e.SheetView.SaveViewState()
        Else
            e.SheetView.LoadViewState(Session(e.SheetView.SheetName))
        End If
        e.Handled = True
     End Sub