ASP.NET w. C#: How To Transfer All Session Variables to XML And SQL?


Can someone please explain how to transfer the variables from a Session object to XML?  

My goal is to store the session variable ojects into SQL server.  I will transfer the variables to XML and then I will send the XML to a stored procedure.  The stored procedure will parse the XML string.  Later, I will need to load the variables that were stored in the SQL database back into Session variables.  I'm trying to store a state to the SQL database and reload that state on demand.  The Session variables will contain only string or int  data.  

Here's a little backround on my programming environment:
-ASP.NET w. C# as the codebehind.  
-Microsoft SQL Server 2003
-Microsoft Development Environment 2002 v. 7.0.9955
-Microsoft .NET Framework 1.0 v. 1.0.3705

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SoCuriousAuthor Commented:
One more thing... I think that somehow I need to transfer the Session variables to a table in Dataset and then convert the Dataset to an XML file.

Out of curiosity, is it absolutely necessary for you to jump through the hoops described above, or will obtaining the end goal of session
state management through SQL Server suffice?
Assuming it is the end goal and not the means that your after, setting up SQLServer
state management is actually pretty straightforward.
Defined in the web.config file of your project is a session state mode attribute:

    <sessionState mode="SqlServer"
      sqlConnectionString="data source=;user id=sa; password="
      cookieless="false" timeout="20" />

You simply provide the data source and login credentials, and ASP.NET takes care of the details (well, the details that don't require
direct handling through user code anyway). It is necessary, however,  to configure the SQL Server with
the appropriate database objects. This is done by creating an "ASPState" database by running the "InstallState.sql" script located in
%WinDir%\Microsoft.Net\Framework\VersionX.X, where X.X refers to the .NET version your project will be using.
Once the server is configured, the application code should run identical to that of the default session state mode,
namely, "InProc."
Keep in mind though that this strategy can have a big impact on your performance, especially if the SQL Server is far away (geographically

Well, I don't know if this will open up your options. Again, the procedure above was from the perspective in that you have a choice
in the implementation details. If not, and you have to get it done by means described above, Lord have mercy on your soul.

I'm gonna hunt around for some useful links and I'll post them shortly.


This is a great article on implementing state management in general. The
SQL Server information is at the very end though.

Hope that some of this helps,

Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

SoCuriousAuthor Commented:

Thanks for responding so promptly.  You wrote, in part "Keep in mind though that this strategy can have a big impact on your performance".  Performance is a big concern so I'm not sure if *Inproc* is an option.  Storing the session state is only needed for special occassions so I'm not sure if the performance trade off is worth it.  My ultimate goal is to have the ability to store/restore a session state to/from a database.  I could enumerate through the session variables and store them to a datatable in a dataset.  Then I could convert the dataset to XML and pass it to a stored procedure.  The process I just described seems like a lot of steps to perform a simple task (store/restore a session state to a database).  I wonder if it is simpler to serialize the session object and pass it to a stored procedure.  


Sorry for the delayed post. Your requirements are a little clearer now and your right, a blanket approach probably wouldn't
best be suited. I came across some reference material regarding using ViewState through ASP.NET. A very informative link I came across is below which provides background, pros and cons as well as a lot of other info. I've never dealt with it personally, but it sounds like it could possibly be built upon to help you achieve your goal.

Also, here's a good article on the relation between DataSets/Tables and XML Schema's as they relate to the .NET architecture.

Sorry I can't be of more assistance, but I don't think your issue will boil down to a 20 line response post.

Hope the links help out,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SoCuriousAuthor Commented:

You have been a great help.  Thanks for providing such useful links.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.