Home » ASP.NET » Storing a DataRow into a Session (server) variable

Storing a DataRow into a Session (server) variable

I recently ran into a situation where I needed to store a DataRow object, which is not serializable, into a Session variable using the Session Server.  As I mentioned yesterday, all sessions should be stored to either the Session Server or the SQL Session Server.  This means that all of the objects being stored must be serializable.

My problem stemmed from the fact that a DataRow is not Serializable and there is no clean mechanism for it to become serializable.

However, a DataTable is serializable.  So, the simple solution is to:

  • Create a DataTable object
  • Add the DataRow to the Table
  • Store the DataTable in the Session variable.

To get the row out, you reverse the process.

  • Retrieve the DataTable from the Session object
  • Cast the object to a DataTable
  • Retrieve the first row from the table.

 

Other post in ASP.NET

Related Post

  • The case of the disappearing session variablesThe case of the disappearing session variables Way back in ASP.NET version 1.1, I wrote one of my first asp.net web sites for a client that depended pretty heavily on session variables.  Without getting into the arguments about the […]
  • Response.Redirect() executes too soon on the Server.Response.Redirect() executes too soon on the Server. I've seen this question a couple of times in various situations. The first involves Javascript and the second involves server side code. Both are caused by a misunderstanding of what this […]
  • Converting a DataList to a ListViewConverting a DataList to a ListView Yesterday, I spent the bulk of the day converting a DataList to a ListView.  I thought I'd spend a little time relating the process for those of you who might be interested in doing […]
  • ASP.NET GridView Edit All Rows At OnceASP.NET GridView Edit All Rows At Once I just saw a question about this yesterday and realized that while I know how to do this, not everyone does.  So, here we go... Here's the problem.  You want to be able to edit […]
  • Multi-Step Processing in ASP.NETMulti-Step Processing in ASP.NET I received the following question a few days ago but I’ve been so busy with billable work that I just haven’t had a chance to answer it until now.  Actually, I’m still busy, but I […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.