I am developing an asp.net web site, using VB.Net as the core language, and have create a class file to store some global methods I use on a number of pages.
When I try to implement a session variable inside a class in this manner:
Imports
Microsoft.VisualBasic
Namespace ChileNavy.global.SessionVars
PublicClass SessionVars
PublicSub initializeVars()
If Session("UserID")
IsNothingThen
Session("UserID") = "SomeDefaultSetting"
EndIf
EndSub
EndClass
EndNamespace
I get an error "Session not declared". Suggesting that the class file sees this is a variable.
Any ideas how I might use Session Variables with a class file?
Perhaps I'm having a bad day but could you give me an example of a vb class (including the imports) where this would work? I'm not exactly sure what you're saying here. I'm very new to asp.net and I guess this is part of the problem...... [:$]
I have a simular problem but am doign it the same way and its not erroring but it is also not getting a value.
I have session variables and one is Session("FtpFolder"). I need this value in the class file so that it knows where to save the file. I can see in the page with trace on that it has
a value. But when I want to use it in the class file it is not getting a value. The simplified code is below.
Imports
Microsoft.VisualBasic
Imports
System
Imports
System.Data
Imports
System.Configuration
Imports
System.Web
Imports
System.Web.Security
Imports
System.Web.UI
Imports
System.Web.UI.WebControls
Imports
System.Web.UI.WebControls.WebParts
Imports
System.Web.UI.HtmlControls
Imports System.IO
Dim strDirectory
As String
strDirectory = System.Web.HttpContext.Current.Session("FtpFolder")
uploadFile.SaveAs(String.Format("{0}{1}", strDirectory, uploadFile.FileName))
jaypea
Member
2 Points
6 Posts
Using Session Variables inside a VB.Net Class File
Apr 02, 2007 12:10 PM|LINK
I am developing an asp.net web site, using VB.Net as the core language, and have create a class file to store some global methods I use on a number of pages.
When I try to implement a session variable inside a class in this manner:
Imports
Microsoft.VisualBasicNamespace ChileNavy.global.SessionVars
Public Class SessionVars
Public Sub initializeVars()
If Session("UserID") Is Nothing Then
Session("UserID") = "SomeDefaultSetting"
End If
End Sub
End Class
End Namespace
I get an error "Session not declared". Suggesting that the class file sees this is a variable.
Any ideas how I might use Session Variables with a class file?
Curt_C
All-Star
66017 Points
7639 Posts
Moderator
Re: Using Session Variables inside a VB.Net Class File
Apr 02, 2007 12:33 PM|LINK
Session is part of the HTTPContext and is derived from System.Web.
Add a reference to System.Web to the project (I'm assuming its just a class project and not in your Web project).
Then add the Imports and/or namespace out the Session object.
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
jaypea
Member
2 Points
6 Posts
Re: Using Session Variables inside a VB.Net Class File
Apr 02, 2007 01:12 PM|LINK
Thanks for the reply I made the change and now it errors with
Reference to a non-shared member requires an object reference
Curt_C
All-Star
66017 Points
7639 Posts
Moderator
Re: Using Session Variables inside a VB.Net Class File
Apr 02, 2007 01:16 PM|LINK
on what line?
Most likely it's just a matter of needing the Session object pathed out, to ensure you are using the Current HTTPContext.
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
jaypea
Member
2 Points
6 Posts
Re: Using Session Variables inside a VB.Net Class File
Apr 02, 2007 01:24 PM|LINK
Perhaps I'm having a bad day but could you give me an example of a vb class (including the imports) where this would work? I'm not exactly sure what you're saying here. I'm very new to asp.net and I guess this is part of the problem...... [:$]
Curt_C
All-Star
66017 Points
7639 Posts
Moderator
Re: Using Session Variables inside a VB.Net Class File
Apr 02, 2007 01:27 PM|LINK
instead of
Session("item")
use
System.Web.HttpContext.Current.Session("item")
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
jaypea
Member
2 Points
6 Posts
Re: Using Session Variables inside a VB.Net Class File
Apr 02, 2007 01:34 PM|LINK
Thanks Curt got it now, works like a charm
[:D]
belcherman
Member
47 Points
112 Posts
Re: Using Session Variables inside a VB.Net Class File
Nov 05, 2007 01:18 PM|LINK
I have session variables and one is Session("FtpFolder"). I need this value in the class file so that it knows where to save the file. I can see in the page with trace on that it has a value. But when I want to use it in the class file it is not getting a value. The simplified code is below.
Imports
Microsoft.VisualBasicImports
SystemImports
System.DataImports
System.ConfigurationImports
System.WebImports
System.Web.SecurityImports
System.Web.UIImports
System.Web.UI.WebControlsImports
System.Web.UI.WebControls.WebPartsImports
System.Web.UI.HtmlControls Imports System.IODim strDirectory As String strDirectory = System.Web.HttpContext.Current.Session("FtpFolder") uploadFile.SaveAs(String.Format("{0}{1}", strDirectory, uploadFile.FileName))
This doesn't work.
Mike
preethi9
Member
5 Points
4 Posts
Re: Using Session Variables inside a VB.Net Class File
Dec 13, 2007 05:11 PM|LINK
Works Perfect.