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

R

R.A.M.

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/
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

R.A.M. said:
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/

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"?
 
R

R.A.M.

Uzytkownik "Göran Andersson said:
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

R.A.M.

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

R.A.M.

U¿ytkownik "R.A.M. said:
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/
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

R.A.M. said:
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/

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.
 
R

R.A.M.

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/
 
R

R.A.M.

Uzytkownik "Göran Andersson said:
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" />
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

R.A.M. said:
I use set/get in code-behind of Default.aspx.

When does the Session object exist?
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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

R.A.M. said:
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.
 
R

R.A.M.

Uzytkownik "Göran Andersson said:
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.
 
R

R.A.M.

Uzytkownik "Göran Andersson said:
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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

R.A.M. said:
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?
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.
 
R

R.A.M.

Uzytkownik "Göran Andersson said:
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");
}
}
}
}
}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

R.A.M. said:
Uzytkownik "Göran Andersson said:
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 ==

So the Session object is not null here?
Admin.DemoUser ?
Admin.DemoDatabase:
Admin.DefaultDatabase);
if (Admin.CurrentDatabase != null)

But here?
{
Admin.ClearLog();
Admin.WriteToLog(Resources.MIM_Magazyn.SelectedDatabase
+ ": " +
Admin.CurrentDatabase);
}
else
{
Session["Error"] =
Resources.MIM_Magazyn.CannotSetupDatabase;
Response.Redirect("Error.aspx");
}
}
}
}
}

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top