Last post Aug 21, 2015 01:31 AM by ngaisteve1
Aug 18, 2015 12:01 AM|ngaisteve1|LINK
Hi, I have some lines of code in several place in my code behind.
Dim path As String
path = Server.MapPath("~/Uploads/") & Session("sUserName") & "/"
So, I move this code up to make path as global variable, but I got this error
unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session
state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.
Line 4: Partial Class UserProfilePhoto
Line 5: Inherits System.Web.UI.Page
Line 6: Dim path As String = Server.MapPath("~/Uploads/") & Session("sUserName") & "/"
Line 8: Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Aug 18, 2015 01:01 AM|markfitzme|LINK
The error essentially states that session has either not been configured in your web.config or that the session entry in the web.config has been turned off. Enable it according to the error instructions and you should be good.
Aug 18, 2015 01:33 AM|francissvk|LINK
As per your description, you should enable the session state in the web.config file:
<sessionState mode="InProc" timeout="20"></sessionState>
Try to include the above statements between the <system.web>tag.
Hope this helps!!
Aug 18, 2015 03:23 AM|ngaisteve1|LINK
But, if I didn't put it at global variable in my page, it works fine.
Aug 18, 2015 03:35 AM|francissvk|LINK
The actual problem is, you are not enabled the "Session" in your web.config file as i mentioned in in my previous post. So in this case , it will throw the same error if you are try to use "Session" variable in your page. Hope that you are using gloabal
variable instead of "Session" variable. Correct me if i'm wrong. :)
Aug 19, 2015 01:14 AM|Weibo Zhang|LINK
I think you’d better create a ReadOnly Property to achieve your need. The following code you could take a look. Beside, you should use Convert.ToString() to change the data type of session value and replace the “/” with “\” in your code.
Public ReadOnly Property path As String
Return Server.MapPath("~/Uploads/") & Convert.ToString(Session("sUserName")) & "\"
For more things about the property, you could refer to the following links.
I hope it’s useful to you.
Aug 21, 2015 01:31 AM|ngaisteve1|LINK
It works. Nice :)