Introduction

This article is a continuation of my previous article on Storing Sessions in a State Server.

I would request readers to have a quick look at my previous article here: Out-of-Process Session State (In State Server) in ASP.NET

In ASP.Net, by default sessions are stored in-proc (i.e. in memory of the Web Server).

Sessions can be stored out-of-process

Let us understand how sessions can be stored in a SQL Server with a sample demo.

To begin with, please download the sample code (OutofProcSessionState.zip) from: Out-of-Process Session State (In State Server) in ASP.NET

Step 1. Open the project.

Step 2. Go to the Web. Config file:

Step 3. Now, go to Login.aspx and run the application from Visual Studio.

sql-out-of-state.gif

Step 4. Click "Go to Home Page" (as per the logic mentioned; the user should go to the new page (Home.aspx) and the user name "Gaurav Singh" should be retrieved from the session.

sql-out-of-state1.gif

Step 5. Not a good-looking screen for developers!! Right… Let's sort it out!!

Step 6. As the error screen says, it was unable to open a particular database (ASPState) in the SQL Server name "Saurabh-PC" which we specified in the web. config.

Step 7. So, what we need to do is to open a Visual Studio Command Prompt to configure the database, as in:

sql-out-of-state2.gif

Step 8. So now the session state should have been added to our database.

Step 9. We can verify this by opening our database as:

sql-out-of-state4.gif

Step 10. Go to Visual Studio and run the application:

Conclusion

So in this article, we have seen how to store a session Out-of-process in SQL Server. I have attached the above demonstration code.

Happy Learning!!