SessionHelper not working

Sam Power asked on May 6, 2014 01:50

In an application I am building where a user can save 0 to many items to their "favourites". As new items are saved the application grabs the existing favourites from the session appends the new one before saving the new items back to session. This is done using the following utility functions provided by Kentico.

CMS.GlobalHelper.SessionHelper.SetValue("favourites", favourites) CMS.GlobalHelper.SessionHelper.GetValue("favourites")

The problem is SetValue doesn't seem to be working. GetValue continues to return null. What am I doing wrong here? Is there some setting that needs to be changed in CMSSiteManager to enable Session. I have set enableSessionState to true in the web.config.

Any help much appreciated, Sam

Correct Answer

Richard Sustek answered on May 18, 2014 04:52

Hi,

After a further investigation I found out that the session is not accesible this way in AppCode folder. The following answer should give you some more information with a possible solution:

http://stackoverflow.com/questions/621549/how-to-access-session-variables-from-any-class-in-asp-net

Kind regards,

Richard Sustek

0 votesVote for this answer Unmark Correct answer

Recent Answers


Richard Sustek answered on May 6, 2014 02:03

Hi Sam,

Thank you for your message.

I cannot seem to reproduce your issue and I was wondering thether your favourites variable is not null in the first place, can you please check it? There is no setting in web.config which you would need to additionaly set in order to get this working I know about.

I have used following code which return the correct "testing session" value out of my session.

 SessionHelper.SetValue("testsession", "testing session");
 string val = SessionHelper.GetValue("testsession").ToString();

Let me know how this code works and if you have further questions.

Kind regards,

Richard Sustek

0 votesVote for this answer Mark as a Correct answer

Sam Power answered on May 6, 2014 02:24

Thanks for the response Richard,

The variable is definitely not null. I tried the code you provided and val came back as null (Well actually it threw an error because the ToString failed because the GetValue returned a null)

When I try and access Session in the traditional way using HttpContext.Current.Session the Session object is null (HttpContext and HttpContext.Current are not). Does that give you any clues as to what may be happening?

Thanks, Sam

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 6, 2014 06:37

What session state are you using Sam? InProc is most common because that's the default but SQL Server is quite a bit more powerful and allows a user to keep session state across a web farm. Could it be your IIS instance is restarting causing session to be lost?

0 votesVote for this answer Mark as a Correct answer

Sam Power answered on May 6, 2014 18:18

Hi Brenden,

Thanks for your message.

At the moment I am using InProc. This list of favourites is being saved only as a string so running session through SQL Server would be an unnecessary overheard, additionally we aren't running the site across a web farm.

I copied and pasted the code from Richard so it was side by side and it still returned null so it's definitely not the IIS instance restarting that is the issue.

I have used SessionHelper previously in a Kentico Web application and it worked fine. This is my first time building a site as a Kentico Website project. So I'm wondering if the difference might lie there somehwere? Also some more information that might help. This code is running from a service I built which is sitting inside the App_Code folder.

Thanks, Sam

0 votesVote for this answer Mark as a Correct answer

reuben sitshetshe answered on June 14, 2016 13:06

hi Richard Sustek SessionHelper.SetValue("testsession", "testing session") and string val = SessionHelper.GetValue("testsession").ToString();

these two methods are not working i tried on kentico the just return null.... please help

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.