473,883 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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.A dd("UserRoles" , arrUserRoles);
Context.User = new
System.Security .Principal.Gene ricPrincipal(Co ntext.User.Iden tity,

In another page I have

string[] arrUserRoles = {}; <-- call this line a
arrUserRoles = (string[]) HttpContext.Cur rent.Items["UserRoles"]; <--- Call
this line b . . . error on this line
Label1.Text = arrUserRoles.Le ngth.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?


Lauchlan M

Nov 17 '05 #1
1 2376

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.


S. Justin Gengo, MCP
Web Developer

Free code library at:

"Out of chaos comes order."
"Lauchlan M" <LM********@Hot mail.com> wrote in message
news:O5******** *******@tk2msft ngp13.phx.gbl.. .

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)


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
be sure to use If Not IsPostBack). This way you can transfer a user from

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

library. Just search the code library for: "server.transfe r".

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: deko | last post by:
I'm getting intermittent "Object Invalid or No Longer Set" errors in my Access 2002 mdb. What causes these errors? Has anyone dealt with this before? I can't trace it because it's not easy to duplicate -- it just show up... I've heard that spurious queries that use Now() or Date() or some other date
by: Lauchlan M | last post by:
Hi. Usin 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
by: Kamal | last post by:
I am trying to send mail through smtp. smtp service is running on my machine. But every time during the smtpmail.send(msg) call gives "Could not access 'CDO.Message' object." error. Could some one help me out to resolve this issue. Thanks in advance, Kamal
by: prince -=nore=- | last post by:
I have a page, where I'm retrieving a file location from a database and outputting it's contents (HTML) onto a web page. On the aspx page, I have referred to the code behind it using the line below. This is where I'm getting the "Object reference not set to an instance.." error <% response.writefile(thelink())%>. The function that line is referring to is below. The record is being retrieved from the database according to its...
by: chuckdfoster | last post by:
I am getting a "Could Not Access CDO.Message Object" Error when I try to use the following code to send an email via ASP.NET. When I run this on one machine it works, on another one it doesn't. Both have VS.NET installed and both have MS Outlook installed. What do I need done to the machine that doesn't work? Or is something wrong with the code? Dim email as New System.Web.Mail.Message email.To = "me@me.com" email.Subject = "Test...
by: Microsoft | last post by:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object" Dim g As System.Drawing.Graphics g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 0, 0) Why is this? Marc
by: dhnriverside | last post by:
Hi peeps I'm just following this HOW-TO from MSDN.. http://support.microsoft.com/default.aspx?scid=kb;en-us;306355 But I've got a problem. I've adding the #using System.Diagnostics; line to my cs file, but when I try and type the following line... Exception objErr = Server.GetLastError().GetBaseException();
by: louie.hutzel | last post by:
This JUST started happening, I don't remember changing any code: When I click the submit button on my form, stuff is supposed to happen (which it does correctly) and a result message is posted back to the same page at the top of my form alerting the user that their request has been approved or denied. However, the end result is an error message: Error Message: Object reference not set to an instance of an object.
by: piyumi80 | last post by:
hi, i write the following code to get a specific data row from the data set.but it generates the "Object reference not set to an instance of an object.".....error private void showClientListForUser() { string fkeys = new string; fkeys = txtUserName.Text; fkeys = txtUserCompanyNumber.Text;
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.