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

"Object reference not set to an instance of an object" error

P: n/a
Hi.

I'm using ASP.NET, getting an "Object reference not set to an instance of an
object" error.

In my login.aspx page I have:

string[] arrUserRoles = new string[] {"UserRole"};
Context.Items.Add("UserRoles", arrUserRoles);
Context.User = new
System.Security.Principal.GenericPrincipal(Context .User.Identity,
arrUserRoles);

In another page I have

string[] arrUserRoles = {}; <-- call this line a
arrUserRoles = (string[]) HttpContext.Current.Items["UserRoles"]; <--- Call
this line b . . . error on this line
Label1.Text = arrUserRoles.Length.ToString();

If I comment out line b it works (displays 0 as it should). If I put line b
back in, it throws the error.

Any suggestions on what's wrong?

Also, how do I test for the context variable arrUserRoles being defined?

Thanks!

Lauchlan M

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Lauchlan,

If there is a specific recommended practice I haven't hear of it yet.

I've been using, as you put it, a "Roll your own" implementation.

The context object is a great way to go in my opinion. There are a lot of
potential pitfalls to using session variables that I just don't like so I
try to stay away from them.

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Lauchlan M" <LM********@Hotmail.com> wrote in message
news:O5***************@tk2msftngp13.phx.gbl...
Thanks!

What is the recommended way of keeping track of this security data?

Is there some provided object that remains static over the session? Or
should I create a session variable for it (which seems to take away from the point of using MS's security framework in the first place and begins to look more like a roll-your-own exercise)

Thanks!

Lauchlan M
The Context object is not like a session variable or application variable. It is a stateless object meaning that it's recreated every request to the server.

However, the context object can be passed from one page to another using
Server.Transfer. Then on the second pages page load (first page load only
so
be sure to use If Not IsPostBack). This way you can transfer a user from

one
page to another and pass values from the first page to the second

through the context object.

I have a some sample code on my website, www.aboutfortunate.com, in the

code
library. Just search the code library for: "server.transfer".


Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.