468,242 Members | 1,687 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

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

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/
Oct 15 '07 #1
14 28553
R.A.M. wrote:
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"?

--
Göran Andersson
_____
http://www.guffa.com
Oct 15 '07 #2
Uzytkownik "Göran Andersson" <gu***@guffa.comnapisal w wiadomosci
news:un**************@TK2MSFTNGP05.phx.gbl...
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/
Oct 15 '07 #3
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/
Oct 15 '07 #4
U¿ytkownik "R.A.M." <r_********@poczta.onet.plnapisa³ w wiadomo¶ci
news:fe**********@news2.task.gda.pl...
>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/
Oct 15 '07 #5
R.A.M. wrote:
Uzytkownik "Göran Andersson" <gu***@guffa.comnapisal w wiadomosci
news:un**************@TK2MSFTNGP05.phx.gbl...
>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/
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
Oct 15 '07 #6
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/
Oct 15 '07 #7
Uzytkownik "Göran Andersson" <gu***@guffa.comnapisal w wiadomosci
news:%2********************@TK2MSFTNGP02.phx.gbl.. .
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" />
Oct 16 '07 #8
R.A.M. wrote:
Uzytkownik "Göran Andersson" <gu***@guffa.comnapisal w wiadomosci
news:%2********************@TK2MSFTNGP02.phx.gbl.. .
>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.

--
Göran Andersson
_____
http://www.guffa.com
Oct 16 '07 #9
R.A.M. wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Oct 16 '07 #10
Uzytkownik "Göran Andersson" <gu***@guffa.comnapisal w wiadomosci
news:%2****************@TK2MSFTNGP04.phx.gbl...
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.
Oct 16 '07 #11

Uzytkownik "Göran Andersson" <gu***@guffa.comnapisal w wiadomosci
news:uB****************@TK2MSFTNGP06.phx.gbl...
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.
Oct 16 '07 #12
R.A.M. wrote:
Uzytkownik "Göran Andersson" <gu***@guffa.comnapisal w wiadomosci
news:%2****************@TK2MSFTNGP04.phx.gbl...
>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.

--
Göran Andersson
_____
http://www.guffa.com
Oct 16 '07 #13
Uzytkownik "Göran Andersson" <gu***@guffa.comnapisal w wiadomosci
news:ed**************@TK2MSFTNGP04.phx.gbl...
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.SelectedDat abase
+ ": " +
Admin.CurrentDatabase);
}
else
{
Session["Error"] =
Resources.MIM_Magazyn.CannotSetupDatabase;
Response.Redirect("Error.aspx");
}
}
}
}
}
Oct 17 '07 #14
R.A.M. wrote:
Uzytkownik "Göran Andersson" <gu***@guffa.comnapisal w wiadomosci
news:ed**************@TK2MSFTNGP04.phx.gbl...
>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.SelectedDat abase
+ ": " +
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.

--
Göran Andersson
_____
http://www.guffa.com
Oct 17 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Spam sucks | last post: by
2 posts views Thread by Luis Esteban Valencia Muñoz | last post: by
3 posts views Thread by KK | last post: by
7 posts views Thread by pmstel | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.