Support of DataView in OutProc Session Model

Sudhakarj21
Posted by in ASP.NET category on for Beginner level | Views : 5969 red flag

.In ASP.NET we will have 2 types od Session Management.

One is InProc where you store the data in the same system memory and the other one is OutProc where the memory will be stored in some other way like State Server or SQL Server. And there are many considerations while using OutProc in this mode we can store classes which serializable only. And most of the things in .NET like DataView, DataRow are not Serializable and cannot be placed in Session in this Mode
Introduction

To Support Non Serailzable type in ASP.NET OutProc Session Mode we need to find an alternate solutions for it. One of the solution is convert that Non Serializable class into a class to Serializable and if the class is not in your control you can implement the below logic.

Say for example you are using a Page in ASP.NET which store some data in session below is the sample that you can use to allow views on this page. If you want to make it applicable to all Pages you just need to put these methods in Global.asax
 
Code

Place the below code on Page Init / Load

 

for (int i = 0; i < Session.Keys.Count; i++)
            {
                if (Session[Session.Keys[i]] is DataTable)
                {
                    if (Session[Session.Keys[i] + "_isView"] != null)
                    {
                        DataTable current = (DataTable)Session[Session.Keys[i]];
                        string[] dataViewOptions = Session[Session.Keys[i] + "_isView"].ToString().Split('|');

                        string rowFilter = dataViewOptions[0];
                        DataViewRowState rowState = (DataViewRowState)Enum.Parse(typeof(DataViewRowState), dataViewOptions[1]);
                        string sort = dataViewOptions[2];
                        Session[Session.Keys[i]] = new DataView(current, rowFilter, sort, rowState);
                        Session[Session.Keys[i] + "_isView"] = null;
                    }
                }
            }

Place the below code on Unload

for (int i = 0; i < Session.Keys.Count; i++)
            {
                if (Session[Session.Keys[i]] is DataView)
                {
                    DataView current = (DataView)Session[Session.Keys[i]];
                    Session[Session.Keys[i]] = current.Table;
                    Session[Session.Keys[i] + "_isView"] = current.RowFilter + "|" + current.RowStateFilter.ToString() + "|" + current.Sort;
                }
            }


Conclusion
You can similarly use the same methods for other Non Serializable types like DataRows etc… This type of logic is very helpful if you don't have access to the classes to modify or make it Serializable. The basic rule is to store the Session Objects in a way that can be retried back
Page copy protected against web site content infringement by Copyscape

About the Author

Sudhakarj21
Full Name: Sudhakar Kottapalli
Member Level: Bronze
Member Status: Member
Member Since: 10/5/2009 7:05:50 AM
Country:



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)