How To Configure SQL Server Session State In ASP.NET Core

How To Configure SQL Server Session State In ASP.NET Core

CheapASPNETHostingReview.com | Best and cheap ASP.NET Core hosting. Session is a feature in ASP.NET Core that enables us to save/store user data. Session stores the data in a dictionary on the Server and SessionId is used as a key. The SessionId is stored on the client at the cookie. The SessionId cookie is sent with every request. The SessionId cookie is Browser specific  and it cannot be shared among the Browsers. There is no timeout specified for SessionId cookie and they are deleted when the Browser session ends.

Session are of two types: InProc or In-memory and OutProc or Distributed session. If our session is in-memory and our Application is hosted on Web-Farm environment, we need to use sticky sessions to tie each session to a specific server. Whereas OutProc session does not require sticky sessions and they are the most preferred way to use session in our application.

Configure SQL Server session state

In SQL Server Session state, previous version of ASP.NET requires a number of tables and stored procedures to manage the storage of session in SQL server and this can be configured, using “aspnet_regsql” command or a tool. ASP.NET Core requires only one table. There are multiple ways to generate this storage table. The first is to use “Microsoft.Extensions.Caching.SqlConfig.Tools” tool. Before we run the commands of these tools, we need to install this tool. To install this tool, we need to add this tool in project.json and perform “dotnet restore” command.
Once this tool is installed for our project, we can execute “sql-cache” command, which generates the required table and an index. The command is in the format given below.
 Here, I am using SQL Express edition and following becomes full command, which generates the table and an index.

 cd

sas

Alternatively, we can create this table and an index manually. The script is given below to create a table and an index.

 SQL session state internally uses memory cache, so as to enable SQL Server session. We need to add “Microsoft.Extensions.Caching.SqlServer” dependency along with “Microsoft.AspNet.Session” into project.json file.

We can add a connection string to appSetting.json file or put it directly in ConfigureServices method of startup class. Following is the definition of ConfigureServices and Configure method of startup class.

Example

In the example given below, I have set my name into session in the first request and retrieved the session value in another request.

 ww

With SQL server state in ASP.NET Core, we can also create and use the session extension methods and we can also store complex objects in to the session.

Summary

This article described the steps required to enable SQL Server as a storage mechanism for session state in an ASP.NET Core MVC Application. The usage of SQL server session state is slightly different than the classic ASP.NET Application.

ASP.NET Tutorial – How to Export Data from SQL Server to Excel in ASP.NET using c#

ASP.NET Tutorial – How to Export Data from SQL Server to Excel in ASP.NET using c#

CheapASPNETHostingReview.com | Best and cheap ASP.NET Hosting. Here I will explain how to export data from sql server to excel in asp.net using c# or export data from sql server database to excel in asp.net using c#.

index

Description:

Now I will explain to you, how to export data from sql server to excel in asp.net using c#.
Before implement this example first design one table UserInformation in your database as shown below
Once table created in database enter some dummy data to test application after that write the following code in your aspx page

Column NameData TypeAllow Nulls
UserIdintYes
UserNamevarchar(50)Yes
Locationvarchar(50)Yes

Now open code behind file and write the following code :

VB.NET