Solved

ASP.NET Session Variables

Posted on 2004-09-22
17
171,625 Views
Last Modified: 2013-11-25
Hi there,
 I have the following code on my page to try and retreive the value of a log in session variable:

<script runat="server">
Sub Page_Load (s as object, e as eventargs)
Dim objChkUsername As String
Dim edit As Integer
objChkUsername = Session("chkusername").ToString()
If objChkUsername Is Nothing Then
edit = 0
Else edit = 1
End If
End Sub
</script>

I get the following error though:

[NullReferenceException: Object reference not set to an instance of an object.]

   ASP.index_aspx.Page_Load(Object s, EventArgs e) in D:\inetpub\wwwroot\moorfieldsresearch.org.uk\process\index.aspx:6
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +750

Can anyone suggest where i am going wrong as i am new to asp.net and can't find any simple enough information on the net!

Thanks,
Richard



 
0
Comment
Question by:crich
  • 5
  • 4
  • 2
  • +5
17 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 12121719
Hi crich,

Probably because you don't specify what namespace the session object is in. Either include it as an import or:

objChkUserName = HttpContext.Current.Session("chkusername").ToString()

Tim Cottee
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 

Author Comment

by:crich
ID: 12121752
I have tried that but i get the same error!
0
 
LVL 7

Expert Comment

by:imsolost
ID: 12121890
From time to time I get the same error if I try to call a session object that has no value.  You might try this

if Session("chkusername").ToString()  &  "" <> "" Then
     objChkUsername = Session("chkusername").ToString()
     Edit = 1
Else
     Edit= 0
End If
0
 

Author Comment

by:crich
ID: 12121991
Still the same error!
0
 

Author Comment

by:crich
ID: 12122007
does anyone know how to check a session variable in asp.net?
0
 
LVL 35

Accepted Solution

by:
YZlat earned 30 total points
ID: 12122145
try this:

<script runat="server">
Sub Page_Load (s as object, e as eventargs)
        Dim objChkUsername As String
        Dim edit As Integer
        objChkUsername = Session("chkusername").ToString()
        If IsDBNull(objChkUsername) Then
            edit = 0
        Else : edit = 1
        End If
    End Sub
</script>
0
 
LVL 35

Expert Comment

by:YZlat
ID: 12122158
<script runat="server">
Sub Page_Load (s as object, e as eventargs)
        Dim objChkUsername As String= Session("chkusername")
        Dim edit As Integer
 
        If IsDBNull(objChkUsername)  Or objChkUsername=" "  Then
              edit = 0
        Else
              edit = 1
        End If
    End Sub
</script>
0
 
LVL 8

Assisted Solution

by:daffodils
daffodils earned 20 total points
ID: 12122312
This is a standard ASP.NET error.. that shows up anytime you call a method like ToString() when the Session variable is empty.

The standard practice is to check for null and then access the Session object properties.

'Check if Session exists
If Not Nothing(Session("chkusername") Then
      ' Not null.. now call the ToString()
      objChkUsername = Session("chkusername").ToString()
      edit = 1
Else
      edit = o
End If


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Expert Comment

by:imsolost
ID: 12122424
First line from daffodils should be

If Not Nothing(Session("chkusername")) Then

Good luck
0
 

Author Comment

by:crich
ID: 12122466
hi there,
Thank you all for helping to work that out, it seems the session variable may have been empty. I will give main points to yzlat and some to daffodils. I have a new problem now however, which i will give out more points for:

I am trying to show text on the webpage if the value of edit = 1 but i get another error:

Name 'edit' is not declared.

with this code:

<% If edit = 1 Then %>
EDIT MODE
<% End If  %>

Apologies in advance for asking what must be very basic questions. I find these are the ones that are very hard to find answers for sometimes!
0
 
LVL 7

Expert Comment

by:imsolost
ID: 12122492
Why not do this server side?  something like this

dim edit as integer

If Edit = 1 then
     label1.text = "whatever you want to say on the page"
Else
    label1.text = "Whatever you say for this condition"
End If
0
 
LVL 7

Expert Comment

by:imsolost
ID: 12122518
It would probably be best if you closed out this questions and started another.  You can reference this one in the new question.
0
 

Author Comment

by:crich
ID: 12122567
0
 
LVL 4

Expert Comment

by:Andre412
ID: 12122586
firstly take the procedure out of the load event just incase the error is to do with the vars in the load procedure call, and set them to run from and onclick event of a button, I say this because it has happened before despite how unlikely it may be.

Are you writing this in the code behind ?

if so then as previously standard practice is to vlidate the source exists prior to setting and objects value to it.

if the session var exists then do something else do something else leave the edit value be

dim edit as integer = 0
if not IsNothing(session("MySessionVar")) then edit=1
0
 
LVL 1

Expert Comment

by:Vikram-Ascendus
ID: 13251824
Hi all, got similar problem on handling of Session in ASP.NET.....

I am using ASP.NET with C#. I always face the problem of session expire. Is there any way of setting session time out duration.
0
 
LVL 4

Expert Comment

by:Andre412
ID: 13256800
its set in the web.config file look it up or post a topic with points
0
 

Expert Comment

by:Tathagat_Chakravarty
ID: 22398557
Dear friend replace your code with the following:
<script runat="server">
Sub Page_Load (s as object, e as eventargs)
Dim objChkUsername As String
Dim edit As Integer
objChkUsername = Convert.ToString(Session("chkusername"))
If objChkUsername Is Nothing Then
edit = 0
Else edit = 1
End If
End Sub
</script>

It will definitely work. Actually your session variable is not initialized prior to use. Thats why it contains 'NULL' value. And when your are going to use it with ToString() method then the exception is occuring. As Tostring() does not handle 'NULL' value. So if you use Convert.ToString() then no exception will occur as it handles 'NULL' automatically. And in your string variable objChkUsername 'NULL' will be assigned. If you are going to assign value to objChkUsername, checking whether Session("chkusername") is null or not and using ToString() there the you are doing same mistake, and exception will occur to check in the if statement.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now