Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

HttpContext.Current.Session==null in .cs file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
R.A.M. - 15 Oct 2007 04:14 GMT
Hello,
I have created ASP.NET project in which I have a file Admin.cs. It contains
static class Admin with some methods and properties. The problem is that in
property get a reference HttpContext.Current.Session is null, although for
instance HttpContext.Current.Application or HttpContext.Current.Request is
not null:
public static class Admin

{

   ...

   set

   {

   HttpContext.Current.Session["Database"] = value;    // HERE OK

   }

   get

   {

   object db = HttpContext.Current.Session["Database"];    // HERE PROBLEM

   if (db == null)

   return null;

   return db.ToString();

   }

   ...

}

Could anyone explain me why? How to use Session object in my code?
/RAM/
Göran Andersson - 15 Oct 2007 06:12 GMT
> Hello,
> I have created ASP.NET project in which I have a file Admin.cs. It contains
[quoted text clipped - 36 lines]
> Could anyone explain me why? How to use Session object in my code?
> /RAM/

Have you debugged the code and verified that it's actually the reference
to the Session object that is null, and not simply that the Items
collection doesn't contain the key "Database"?

Signature

Göran Andersson
_____
http://www.guffa.com

R.A.M. - 15 Oct 2007 06:26 GMT
> Have you debugged the code and verified that it's actually the reference
> to the Session object that is null, and not simply that the Items
> collection doesn't contain the key "Database"?

Yes, I debugged the code. HttpContext.Current.Application,
HttpContext.Current.Cache, HttpContext.Current.Response,
HttpContext.Current.Request are not null, they reference valid object. Only
HttpContext.Current.Session is null.
It looks strange for me, so I have sent the post.
Could you help me?
/RAM/
R.A.M. - 15 Oct 2007 06:38 GMT
I have forgotten to add: I changed web server from IIS 5.1 to Cassini 2.
Generally Cassini works fine, but maybe that's the reson why Session doesn't
work... I cannot use IIS because I had interal error in IIS and I failed to
solve the problem.
/RAM/
R.A.M. - 15 Oct 2007 13:30 GMT
>I have forgotten to add: I changed web server from IIS 5.1 to Cassini 2.
>Generally Cassini works fine, but maybe that's the reson why Session
>doesn't work... I cannot use IIS because I had interal error in IIS and I
>failed to solve the problem.
No, that's not the reson. I have learnt that Cassini supports Session
object.
/RAM/
Göran Andersson - 15 Oct 2007 18:08 GMT
>> Have you debugged the code and verified that it's actually the reference
>> to the Session object that is null, and not simply that the Items
[quoted text clipped - 7 lines]
> Could you help me?
> /RAM/

In your code you indicated that the Session object existed when you set
the session variable, but not when you read it? It doesn't make sense
that the existance of the Session object would be different depending on
if you try to read or write a value. Where do you use the property? When
does the Session object exist?

The web site can have session state enabled or disabled, but that is a
global setting for the site. If disabled, there won't be any Session
objects at all.

Signature

Göran Andersson
_____
http://www.guffa.com

R.A.M. - 16 Oct 2007 06:07 GMT
> In your code you indicated that the Session object existed when you set
> the session variable, but not when you read it? It doesn't make sense that
> the existance of the Session object would be different depending on if you
> try to read or write a value. Where do you use the property? When does the
> Session object exist?
I use set/get in code-behind of Default.aspx.

> The web site can have session state enabled or disabled, but that is a
> global setting for the site. If disabled, there won't be any Session
> objects at all.
I have session enabled in my web.config:
<sessionState mode="InProc" timeout="20" />
Göran Andersson - 16 Oct 2007 11:10 GMT
>> In your code you indicated that the Session object existed when you set
>> the session variable, but not when you read it? It doesn't make sense that
>> the existance of the Session object would be different depending on if you
>> try to read or write a value. Where do you use the property? When does the
>> Session object exist?
> I use set/get in code-behind of Default.aspx.

When does the Session object exist?

>> The web site can have session state enabled or disabled, but that is a
>> global setting for the site. If disabled, there won't be any Session
>> objects at all.
> I have session enabled in my web.config:
> <sessionState mode="InProc" timeout="20" />

If the session state is disabled in IIS, the setting in web.config
doesn't have any effect.

Signature

Göran Andersson
_____
http://www.guffa.com

R.A.M. - 16 Oct 2007 15:23 GMT
> When does the Session object exist?
I am not sure if I understand you... I can use Session from Default.aspx in
Admin...get. In Admin...set it is not accessible.

> If the session state is disabled in IIS, the setting in web.config doesn't
> have any effect.
I don't use IIS, because it didn't work on my computer. I use Cassini v2 on
Windows XP Professional.
Göran Andersson - 16 Oct 2007 19:05 GMT
>> When does the Session object exist?
> I am not sure if I understand you... I can use Session from Default.aspx in
> Admin...get. In Admin...set it is not accessible.

That doesn't make sense at all. Can you show the code that is using the
properties?

>> If the session state is disabled in IIS, the setting in web.config doesn't
>> have any effect.
> I don't use IIS, because it didn't work on my computer. I use Cassini v2 on
> Windows XP Professional.

Which should have the corresponding setting.

Signature

Göran Andersson
_____
http://www.guffa.com

R.A.M. - 17 Oct 2007 12:54 GMT
> That doesn't make sense at all.
But it's true.

> Can you show the code that is using the properties?
Here you are (property Admin.CurrentDatabase):

public partial class DefaultPage : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           // Display message:
           if (Session["Message"] != null)
               MessageLabel.Text = Session["Message"].ToString();
           else
               MessageLabel.Text = String.Empty;
           // Display error:
           if (Session["Error"] != null)
               ErrorLabel.Text = Session["Error"].ToString();
           else
               ErrorLabel.Text = String.Empty;
           // Setting up the database:
           if (Admin.CurrentDatabase == null)
           {
               Admin.CurrentDatabase = (Admin.CurrentUserName ==
Admin.DemoUser ?
                                        Admin.DemoDatabase:
                                        Admin.DefaultDatabase);
               if (Admin.CurrentDatabase != null)
               {
                   Admin.ClearLog();
                   Admin.WriteToLog(Resources.MIM_Magazyn.SelectedDatabase
+ ": " +
                                    Admin.CurrentDatabase);
               }
               else
               {
                   Session["Error"] =
Resources.MIM_Magazyn.CannotSetupDatabase;
                   Response.Redirect("Error.aspx");
               }
           }
       }
   }
}
Göran Andersson - 17 Oct 2007 17:27 GMT
>> That doesn't make sense at all.
> But it's true.
[quoted text clipped - 22 lines]
>             {
>                 Admin.CurrentDatabase = (Admin.CurrentUserName ==

So the Session object is not null here?

> Admin.DemoUser ?
>                                          Admin.DemoDatabase:
>                                          Admin.DefaultDatabase);
>                 if (Admin.CurrentDatabase != null)

But here?

>                 {
>                     Admin.ClearLog();
[quoted text clipped - 12 lines]
>     }
> }

If that is in fact true, then your installation is not capable of
running applications. It's seriously broken, and in need of repair or
reinstallation.

Signature

Göran Andersson
_____
http://www.guffa.com

R.A.M. - 15 Oct 2007 18:15 GMT
I have learnt that accessibilty of HttpContext.Current.Session may depend on
the place I use Admin...get.
But in my project it is Default.aspx page (startup), method Page_Load
(code-behind). So I have a question: should HttpContext.Current.Session be
accessible in this place?
/RAM/
Göran Andersson - 16 Oct 2007 11:15 GMT
> I have learnt that accessibilty of HttpContext.Current.Session may depend on
> the place I use Admin...get.

There is only a HttpContext if the code is executed to handle a request.
The Application_End event for example is not executed to handle a
request, and doesn't have a HttpContext.

> But in my project it is Default.aspx page (startup), method Page_Load
> (code-behind). So I have a question: should HttpContext.Current.Session be
> accessible in this place?

Yes. If session state is enabled for the site.

Signature

Göran Andersson
_____
http://www.guffa.com

R.A.M. - 16 Oct 2007 15:25 GMT
> Yes. If session state is enabled for the site.

I haven't disabled session state for the site. I even don't know how to do
it.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.