Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 14th, 2004, 11:43 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using Session in Classes

I was under the (incorrect) impression that use of the Session object was no different in a class definition than in a page code-behind file.

The code I'm using is along these lines:

If Session("UserID") Is Nothing Then
   'some code
End If

I get an error saying that the name 'Session' is not declared. Something's up. I would think that referencing a simple session value would be rather straightforward. Any ideas?


- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old January 14th, 2004, 11:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Did you update the web.config file to allow sessions?

<configuration>
<system.web>
<sessionState
            mode="InProc"
            cookieless="false"
            timeout="20"
    />
</system.web>
</configuration>

 
Old January 14th, 2004, 12:11 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Stu-

Yes, the web.config file is set up properly to allow sessions, and I am using them without issues on my pages.

-Colonel

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old January 14th, 2004, 12:28 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I was able to do a concise workaround.

Instead of using the Session explicitly in the class file, I passed the value in as a parameter from the calling page. This worked perfectly. (I guess workaround is too strong a term here - this is probably how I should have done this in the first place)

However, I would still like to know what the deal is with the Session in class definitions.


- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old January 14th, 2004, 01:56 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Colonel,

When you create an ASPX page or ASCX user control (and code-behind class), the parent classes are System.Web.UI.Page and System.Web.UI.UserControl, respectively. Each of these classes have public properties for Session, Request, Server and so-on. This gives you access to the instance of each object within the context of the current http request.

However, when you create a stand-alone class, that class has no knowledge of the instances of those objects. So you need to use the "current context":

System.Web.HttpContext.Current.Request
System.Web.HttpContext.Current.Server
System.Web.HttpContext.Current.Session

This will give you the instance of each object for the current http request.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 14th, 2004, 02:25 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peter, that's some critical info.

Once again, You.TheMan = True :)

-Colonel

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old June 10th, 2004, 12:59 AM
Authorized User
 
Join Date: Jun 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm sorry Guys, i'm still not able to access my session value from a class file. Even after using System.Web.HttpContext.Current.Session

Any Idea???

Thanks in advance
 
Old June 10th, 2004, 07:36 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Where is this class living? Is it in the web application project or a class library? If it's in a class library then you need to make sure that the library has a reference to System.Web. If the class is in the web application project or you already have the correct reference in the class library then I'm not sure what the problem is.
 
Old June 11th, 2004, 12:32 AM
Authorized User
 
Join Date: Jun 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes the Class File is in web Application project only. And the reference to "System.Web" is already there. Even then I'm not able to. I am assigning the value to the session variable in an ascx page, not in aspx page. Will this matter?

 
Old June 11th, 2004, 07:52 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can access Session from both a page and a user control in the same way. However you are saying you can't access it from a class. Can you provide more detail on the error you are getting?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using classes seymour_glass C# 1 April 26th, 2007 10:18 PM
Session Variables in C# Classes rlb ASP.NET 2.0 Basics 3 April 5th, 2007 02:15 PM
classes dhoward VB.NET 2002/2003 Basics 2 November 8th, 2006 10:35 AM
session and cookie problem (empty session file) msincan BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 February 27th, 2005 05:31 PM
Get session values from a classes minhpx General .NET 3 August 9th, 2004 10:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.