Click here to Skip to main content
13,299,252 members (44,290 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 20 Sep 2007

ASP.NET session state store provider for MySQL

, 18 Oct 2007
Rate this:
Please Sign up or sign in to vote.
A brief description on how to set-up MySQL as session state store for ASP.NET.


This article describes how to set-up an ASP.NET project in order to use MySQL as its session state store provider.

Using the code

Looking on the web, I was not able to find a suitable solution that uses MySQL as its session state store. It may certainly be possible that there is already a better solution available (if so, please do not hesitate to contact me). However, I was able to find a sample session state store provider using MS Access. That's it! Port it to MySQL... and that's what I did.

But, let's do it step by step. The list below gives you an idea of what I did in order to get it to work:

  1. Downloaded the sample session state store provider for MS Access from MSDN.
  2. Downloaded the .NET Connector (using version 5.0.7) from MySQL.
  3. Ported the sample code provided by Microsoft for use with MySQL (the zipped source code can be found here).
  4. Adjusted the Web.config accordingly:
  5. <configuration xmlns=""> 
        <add name="MySqlSessionServices" 
          connectionString="Database=<name of database>; 
                Data Source=<host>; User Id=<login>; Password=<password>"/> 
        <sessionState cookieless="false" regenerateExpiredSessionId="true" 
                   mode="Custom" customProvider="MySqlSessionProvider"> 
            <add name="MySqlSessionProvider" 

You can check it out at ... it works like a charm.

Please do not hesitate to contact me if you have any questions and/or comments.


  • 2007-09-20: Initial version.
  • 2007-10-17: Updated link to sources.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Harry Kimpel
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionMySql.Data.MySqlClient.MySqlException (0x80004005): Variable 'sql_select_limit' can't be set to the value of '-1' Pin
Vipul_Ch5-May-15 5:19
memberVipul_Ch5-May-15 5:19 
Questionplz brack main code Pin
saqibali211918-Aug-11 9:02
membersaqibali211918-Aug-11 9:02 
GeneralGreat! [modified] Pin
devjunkie11-Jan-09 1:19
memberdevjunkie11-Jan-09 1:19 
GeneralBug... [modified] Pin
Johannes751-May-08 0:44
memberJohannes751-May-08 0:44 

Thanks for a great script. Exactly what I needed Smile | :)
However, I had an annoying bug, took me forever to find.....

Row 534 in your script:
cmd.Parameters.Add ("?Expires", MySqlDbType.Datetime).Value =<br />
              DateTime.Now.AddMinutes (pConfig.Timeout.Minutes);

I had to change pConfig.Timeout.Minutes to pConfig.Timeout.TotalMinutes because my timeout is set to whole days which means that pConfig.Timeout.Minutes = 0.
And because of that, in row 453
item = CreateNewStoreData(context,pConfig.Timeout.TotalMinutes);
The double pConfig.Timeout.TotalMinutes needed to be cast to an int because pConfig.Timeout.Minutes is an int.

[Later added]
And I also found on row 577, in the SQL command
"DELETE * FROM <br />
. It should be "DELETE FROM sessions..."
without the '*'.
[/Later added]

modified on Thursday, May 1, 2008 7:00 AM

QuestionWill this work for ASP.NET 1.1? Pin
ajdiaz3-Jan-08 7:03
memberajdiaz3-Jan-08 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 18 Oct 2007
Article Copyright 2007 by Harry Kimpel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid