Object reference

  • Thread starter Thread starter simon
  • Start date Start date
S

simon

I get an error message:
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

I get an error in line:
If Session("user") Is Nothing Then....

What could be wrong?
 
Apparently, the Session doesn't exist in the context of the line you posted.

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Hi,
I get an error in line:
If Session("user") Is Nothing Then....

What could be wrong?

Session object is null (Nothing).

1) Check <sessionState> element in your Web.config file.
2) Make sure that you retrieve it correctly.
 
Hi,

If I use session object in user control(aspx page), it doesn't work.
If I write the same code in default.aspx then everything is fine.

In past I have code and all my functions in include.asp page, which I
included on all my pages.
I had line:
If Session("user") Is Nothing Then
response.redirect "login.asp"
End If

Now I move all my functions to user control (by the book) and register it on
all my pages - approximate the similar like with include.
But if the session won't work, I don't want to write line with session
checking on all my pages.
Should I use include statement(which is past from asp) or is there some
other way?

Thank you,
Simon
 
Hi,
I had line:
If Session("user") Is Nothing Then
response.redirect "login.asp"
End If

Now I move all my functions to user control (by the book) and register it on
all my pages - approximate the similar like with include.

Try to check

HttpContext.Current.Session("user") instead.
 
I have control leviMenu.ascx, where I define session:
If HttpContext.Current.Session("user") Is Nothing Then
Dim uporabnik(2)
uporabnik(0) = "1"
uporabnik(1) = "Simon Zupan"
uporabnik(2) = "2"
Session("user") = uporabnik
End If

Then I register it on the page:

<%@ Register TagPrefix ="Menu" TagName = "LeviMenu" Src =
"userControls\leviMenu.ascx"%>

then use it on page:

<menu:leviMenu runat="server" ID="Levimenu1"></menu:leviMenu>

Then on the same page in Private Sub Page_Load event, I'm reading the value
from session:

HttpContext.Current.Session("user")(0)

And I gett an error message:

System.NullReferenceException: Object variable or With block variable not
set.

The session object is not recognized.

How can I solve this problem?

I have session object in user control because it's registered on all pages.
I can write the session code on each page separeted, it works than, but than
I don't have the code on one place, it doesn't make any sence.

Thank you,
Simon
 
Back
Top