I have tried that but i get the same error!
Main Topics
Browse All TopicsHi 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").ToS
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(O
System.Web.UI.Control.OnLo
System.Web.UI.Control.Load
System.Web.UI.Page.Process
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
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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("chkuserna
' Not null.. now call the ToString()
objChkUsername = Session("chkusername").ToS
edit = 1
Else
edit = o
End If
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!
New thread for this question:
http://www.experts-exchang
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("MySessi
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("
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.
Business Accounts
Answer for Membership
by: TimCotteePosted on 2004-09-22 at 04:25:17ID: 12121719
Hi crich,
n("chkuser name").ToS tring()
Probably because you don't specify what namespace the session object is in. Either include it as an import or:
objChkUserName = HttpContext.Current.Sessio
Tim Cottee
Brainbench MVP for Visual Basic
http://www.brainbench.com