By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,948 Members | 1,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,948 IT Pros & Developers. It's quick & easy.

Session variables in a Public Class?

P: n/a
Hi,

How can I access a session variable within a Public Class?
I have tried the below code, but I get a server error "Object reference not
set to an instance of an object".

System.Web.HttpContext.Current.Session.Add("myKey" , "myValue")
Dim a As String
a = System.Web.HttpContext.Current.Session.Item("myKey ")

Thanks,
Thomas Andersson

Nov 19 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"Thomas Andersson" <Thomas An*******@discussions.microsoft.com> wrote in
message news:5C**********************************@microsof t.com...

Try this:

System.Web.HttpContext.Current.Session("myKey") = "myValue"
Dim a As String
a = System.Web.HttpContext.Current.Session("myKey").To String()

Nov 19 '05 #2

P: n/a
Hi,

Thank you for your suggestion, but this code generate the same error "Object
reference not set to an instance of an object".

Best regards,
Thomas Andersson

"Mark Rae" wrote:
"Thomas Andersson" <Thomas An*******@discussions.microsoft.com> wrote in
message news:5C**********************************@microsof t.com...

Try this:

System.Web.HttpContext.Current.Session("myKey") = "myValue"
Dim a As String
a = System.Web.HttpContext.Current.Session("myKey").To String()

Nov 19 '05 #3

P: n/a
"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in
message news:C6**********************************@microsof t.com...
Thank you for your suggestion, but this code generate the same error
"Object
reference not set to an instance of an object".


Does the error occur on the 1st or 3rd line?
Nov 19 '05 #4

P: n/a
In both lines, if I remove the first line the error occur in the 3rd line...

"Mark Rae" wrote:
"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in
message news:C6**********************************@microsof t.com...
Thank you for your suggestion, but this code generate the same error
"Object
reference not set to an instance of an object".


Does the error occur on the 1st or 3rd line?

Nov 19 '05 #5

P: n/a
"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in
message news:DE**********************************@microsof t.com...
In both lines, if I remove the first line the error occur in the 3rd
line...


Hmm - presumably, you can access the Session object from other places i.e.
an ASPX page's code-behind...?
Nov 19 '05 #6

P: n/a
Hi Mark,

I can access the session variables from all my aspx pages in the code behind.
But in the public class it is not possible...
I can also access the session variables in the Global.asax, except in the
Sub Application_BeginRequest.
What can be the problem?

Best regards,
Thomas Andersson
"Mark Rae" wrote:
"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in
message news:DE**********************************@microsof t.com...
In both lines, if I remove the first line the error occur in the 3rd
line...


Hmm - presumably, you can access the Session object from other places i.e.
an ASPX page's code-behind...?

Nov 19 '05 #7

P: n/a
There's no problem. That behavior is by design.

The Session object is not available within the
context of Sub Application_BeginRequest.

Check out Scott Allen's article on the HttpContext Items Collection :
http://odetocode.com/Articles/111.aspx

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in message
news:1D**********************************@microsof t.com...
Hi Mark,

I can access the session variables from all my aspx pages in the code behind.
But in the public class it is not possible...
I can also access the session variables in the Global.asax, except in the
Sub Application_BeginRequest.
What can be the problem?

Best regards,
Thomas Andersson
"Mark Rae" wrote:
"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in
message news:DE**********************************@microsof t.com...
> In both lines, if I remove the first line the error occur in the 3rd
> line...


Hmm - presumably, you can access the Session object from other places i.e.
an ASPX page's code-behind...?

Nov 19 '05 #8

P: n/a
Hi Juan T. Llibre,

Thank's for the information...
But should it not be possible to access the session variables in a public
class?

Best regards,
Thomas Andersson

"Juan T. Llibre" wrote:
There's no problem. That behavior is by design.

The Session object is not available within the
context of Sub Application_BeginRequest.

Check out Scott Allen's article on the HttpContext Items Collection :
http://odetocode.com/Articles/111.aspx

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in message
news:1D**********************************@microsof t.com...
Hi Mark,

I can access the session variables from all my aspx pages in the code behind.
But in the public class it is not possible...
I can also access the session variables in the Global.asax, except in the
Sub Application_BeginRequest.
What can be the problem?

Best regards,
Thomas Andersson
"Mark Rae" wrote:
"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in
message news:DE**********************************@microsof t.com...

> In both lines, if I remove the first line the error occur in the 3rd
> line...

Hmm - presumably, you can access the Session object from other places i.e.
an ASPX page's code-behind...?


Nov 19 '05 #9

P: n/a
You are accessing the session variables in the code behind.
i.e., you can set and get session variables in code.

What else do you want to do with session variables in code ?
Where else, if not in code-behind, do you want to access them from ?


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in message
news:09**********************************@microsof t.com...
Hi Juan T. Llibre,

Thank's for the information...
But should it not be possible to access the session variables in a public
class?

Best regards,
Thomas Andersson

"Juan T. Llibre" wrote:
There's no problem. That behavior is by design.

The Session object is not available within the
context of Sub Application_BeginRequest.

Check out Scott Allen's article on the HttpContext Items Collection :
http://odetocode.com/Articles/111.aspx

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in message
news:1D**********************************@microsof t.com...
> Hi Mark,
>
> I can access the session variables from all my aspx pages in the code behind.
> But in the public class it is not possible...
> I can also access the session variables in the Global.asax, except in the
> Sub Application_BeginRequest.
> What can be the problem?
>
> Best regards,
> Thomas Andersson
>
>
> "Mark Rae" wrote:
>
>> "Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in
>> message news:DE**********************************@microsof t.com...
>>
>> > In both lines, if I remove the first line the error occur in the 3rd
>> > line...
>>
>> Hmm - presumably, you can access the Session object from other places i.e.
>> an ASPX page's code-behind...?
>>
>>
>>


Nov 19 '05 #10

P: n/a
from a class that exists in an external class library and that is not
derived from Page or IHttpHandler or IHttpModule etc
"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:uN*************@TK2MSFTNGP12.phx.gbl...
You are accessing the session variables in the code behind.
i.e., you can set and get session variables in code.

What else do you want to do with session variables in code ?
Where else, if not in code-behind, do you want to access them from ?


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in message news:09**********************************@microsof t.com...
Hi Juan T. Llibre,

Thank's for the information...
But should it not be possible to access the session variables in a public class?

Best regards,
Thomas Andersson

"Juan T. Llibre" wrote:
There's no problem. That behavior is by design.

The Session object is not available within the
context of Sub Application_BeginRequest.

Check out Scott Allen's article on the HttpContext Items Collection :
http://odetocode.com/Articles/111.aspx

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in message news:1D**********************************@microsof t.com...
> Hi Mark,
>
> I can access the session variables from all my aspx pages in the code behind. > But in the public class it is not possible...
> I can also access the session variables in the Global.asax, except in the > Sub Application_BeginRequest.
> What can be the problem?
>
> Best regards,
> Thomas Andersson
>
>
> "Mark Rae" wrote:
>
>> "Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in >> message news:DE**********************************@microsof t.com...
>>
>> > In both lines, if I remove the first line the error occur in the 3rd >> > line...
>>
>> Hmm - presumably, you can access the Session object from other places i.e. >> an ASPX page's code-behind...?
>>
>>
>>


Nov 19 '05 #11

P: n/a
Given that Session is a property set via System.Web.UI.Page,
you'll have a hard time implementing a class which is not
derived from Page and which enables Session management.

You might find it worthwhile to read through the
several Session projects at the Code Project :

http://www.codeproject.com/aspnet/#S...+Session+State

They'll give you an idea of what others have attenmpted to do.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"gerry" <ge**@hotmail.com> wrote in message news:Ot**************@TK2MSFTNGP12.phx.gbl...
from a class that exists in an external class library and that is not
derived from Page or IHttpHandler or IHttpModule etc
"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:uN*************@TK2MSFTNGP12.phx.gbl...
You are accessing the session variables in the code behind.
i.e., you can set and get session variables in code.

What else do you want to do with session variables in code ?
Where else, if not in code-behind, do you want to access them from ?


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in

message
news:09**********************************@microsof t.com...
> Hi Juan T. Llibre,
>
> Thank's for the information...
> But should it not be possible to access the session variables in a public > class?
>
> Best regards,
> Thomas Andersson
>
> "Juan T. Llibre" wrote:
>
>> There's no problem. That behavior is by design.
>>
>> The Session object is not available within the
>> context of Sub Application_BeginRequest.
>>
>> Check out Scott Allen's article on the HttpContext Items Collection :
>> http://odetocode.com/Articles/111.aspx
>>
>>
>>
>> Juan T. Llibre
>> ASP.NET MVP
>> http://asp.net.do/foros/
>> Foros de ASP.NET en Español
>> Ven, y hablemos de ASP.NET...
>> ======================
>>
>> "Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in message >> news:1D**********************************@microsof t.com...
>> > Hi Mark,
>> >
>> > I can access the session variables from all my aspx pages in the code behind. >> > But in the public class it is not possible...
>> > I can also access the session variables in the Global.asax, except in the >> > Sub Application_BeginRequest.
>> > What can be the problem?
>> >
>> > Best regards,
>> > Thomas Andersson
>> >
>> >
>> > "Mark Rae" wrote:
>> >
>> >> "Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in >> >> message news:DE**********************************@microsof t.com...
>> >>
>> >> > In both lines, if I remove the first line the error occur in the 3rd >> >> > line...
>> >>
>> >> Hmm - presumably, you can access the Session object from other places i.e. >> >> an ASPX page's code-behind...?
>> >>
>> >>
>> >>
>>
>>
>>



Nov 19 '05 #12

P: n/a
Hi Juan,

Thank you very much for your help and tips.

Best regards,
Thomas Andersson

"Juan T. Llibre" wrote:
Given that Session is a property set via System.Web.UI.Page,
you'll have a hard time implementing a class which is not
derived from Page and which enables Session management.

You might find it worthwhile to read through the
several Session projects at the Code Project :

http://www.codeproject.com/aspnet/#S...+Session+State

They'll give you an idea of what others have attenmpted to do.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"gerry" <ge**@hotmail.com> wrote in message news:Ot**************@TK2MSFTNGP12.phx.gbl...
from a class that exists in an external class library and that is not
derived from Page or IHttpHandler or IHttpModule etc
"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:uN*************@TK2MSFTNGP12.phx.gbl...
You are accessing the session variables in the code behind.
i.e., you can set and get session variables in code.

What else do you want to do with session variables in code ?
Where else, if not in code-behind, do you want to access them from ?


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in

message
news:09**********************************@microsof t.com...
> Hi Juan T. Llibre,
>
> Thank's for the information...
> But should it not be possible to access the session variables in a

public
> class?
>
> Best regards,
> Thomas Andersson
>
> "Juan T. Llibre" wrote:
>
>> There's no problem. That behavior is by design.
>>
>> The Session object is not available within the
>> context of Sub Application_BeginRequest.
>>
>> Check out Scott Allen's article on the HttpContext Items Collection :
>> http://odetocode.com/Articles/111.aspx
>>
>>
>>
>> Juan T. Llibre
>> ASP.NET MVP
>> http://asp.net.do/foros/
>> Foros de ASP.NET en Español
>> Ven, y hablemos de ASP.NET...
>> ======================
>>
>> "Thomas Andersson" <Th*************@discussions.microsoft.com> wrote in

message
>> news:1D**********************************@microsof t.com...
>> > Hi Mark,
>> >
>> > I can access the session variables from all my aspx pages in the code

behind.
>> > But in the public class it is not possible...
>> > I can also access the session variables in the Global.asax, except in

the
>> > Sub Application_BeginRequest.
>> > What can be the problem?
>> >
>> > Best regards,
>> > Thomas Andersson
>> >
>> >
>> > "Mark Rae" wrote:
>> >
>> >> "Thomas Andersson" <Th*************@discussions.microsoft.com> wrote

in
>> >> message news:DE**********************************@microsof t.com...
>> >>
>> >> > In both lines, if I remove the first line the error occur in the

3rd
>> >> > line...
>> >>
>> >> Hmm - presumably, you can access the Session object from other

places i.e.
>> >> an ASPX page's code-behind...?
>> >>
>> >>
>> >>
>>
>>
>>



Nov 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.