Click here to Skip to main content
Email Password   helpLost your password?

Introduction

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 on 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:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

<connectionStrings>

<add name="MySqlSessionServices" connectionString="Database=<name of database>; Data Source=<host>; User Id=<login>; Password=<password>"/>

</connectionStrings>

<system.web>

<sessionState cookieless="false" regenerateExpiredSessionId="true" mode="Custom" customProvider="MySqlSessionProvider">

<providers>

<add name="MySqlSessionProvider" type="Samples.AspNet.Session.MySqlSessionStateStore" connectionStringName="MySqlSessionServices" writeExceptionsToEventLog="false"/>
</providers>

</sessionState>

</system.web>

</configuration>

You can check it out at www.kimpel.com ... it works like a charm .

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

History

2007-09-20: initial version

2007-10-17: updated link to sources

<script type="text/javascript" src="http://i.ixnp.com/shot_main_js/v2.26.1/"></script><script type="text/javascript" src="http://shots.snap.com//snap_shots.js?ro=1&ap=1&tc=0&tp=1&hdd=1500&si=0&key=38278c431bc885d2e8b29b587e4c5d90&th=silver&sb=1&link_icon=on&shots_trigger=both&size=small&lang=en-us&campaign=addon_ff_1.3.0&plugin=1"></script>
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralGreat! [modified]
devjunkie
1:19 11 Jan '09  
Great article!
Thanks a lot!

Thanks to Johannes as well for solving some issues too! Big Grin

I found yet another issue since i wanted a timeout of 60 minutes.
Line 680:
cmd.Parameters.Add("?Expires", MySqlDbType.Datetime).Value
= DateTime.Now.AddMinutes(pConfig.Timeout.Minutes);

i needed to change this to
cmd.Parameters.Add("?Expires", MySqlDbType.Datetime).Value
= DateTime.Now.AddMinutes(Convert.ToInt32(pConfig.Timeout.TotalMinutes));

otherwise my timeout was set to the creationtime of the session.

// Peter

modified on Sunday, January 11, 2009 6:53 AM

GeneralBug... [modified]
Johannes75
0:44 1 May '08  
Hi!

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 =
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
sessions..."
. It should be "DELETE FROM sessions..."
without the '*'.
[/Later added]
/Johannes

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

QuestionWill this work for ASP.NET 1.1?
ajdiaz
7:03 3 Jan '08  
Will this work for ASP.NET 1.1? Or is this strictly for ASP.NET 2.0?


Last Updated 18 Oct 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010