.NET Framework - HttpContext.Current.Session==null in .cs file

Asked By R.A.M. on 14-Oct-07 11:14 PM
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/




guff replied on 15-Oct-07 01:12 AM
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"?

--
Göran Andersson
_____
http://www.guffa.com
R.A.M. replied on 15-Oct-07 01:26 AM
Uzytkownik "Göran Andersson" <guffa@guffa.com> napisal w wiadomosci

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. replied on 15-Oct-07 01:38 AM
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. replied on 15-Oct-07 08:30 AM
U¿ytkownik "R.A.M." <r_ahimsa_m@poczta.onet.pl> napisa³ w wiadomo¶ci
No, that is not the reson. I have learnt that Cassini supports Session
object.
/RAM/
guff replied on 15-Oct-07 01:08 PM
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.

--
Göran Andersson
_____
http://www.guffa.com
R.A.M. replied on 15-Oct-07 01:15 PM
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.A.M. replied on 16-Oct-07 01:07 AM
I use set/get in code-behind of Default.aspx.

I have session enabled in my web.config:
guff replied on 16-Oct-07 06:10 AM
When does the Session object exist?


If the session state is disabled in IIS, the setting in web.config
does not have any effect.

--
Göran Andersson
_____
http://www.guffa.com
guff replied on 16-Oct-07 06:15 AM
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.


Yes. If session state is enabled for the site.

--
Göran Andersson
_____
http://www.guffa.com
R.A.M. replied on 16-Oct-07 10:23 AM
Uzytkownik "Göran Andersson" <guffa@guffa.com> napisal w wiadomosci
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.

I don't use IIS, because it didn't work on my computer. I use Cassini v2 on
Windows XP Professional.
R.A.M. replied on 16-Oct-07 10:25 AM
I have not disabled session state for the site. I even do not know how to do
it.
guff replied on 16-Oct-07 02:05 PM
That does not make sense at all. Can you show the code that is using the
properties?


Which should have the corresponding setting.

--
Göran Andersson
_____
http://www.guffa.com
R.A.M. replied on 17-Oct-07 07:54 AM
Uzytkownik "Göran Andersson" <guffa@guffa.com> napisal w wiadomosci
But it's true.

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");
}
}
}
}
}
guff replied on 17-Oct-07 12:27 PM
So the Session object is not null here?


But here?


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.

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