473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Confused - control not set to instance inside foreach loop - please help

Hello,

I have code like the following...

foreach (Control ctl in Page.Controls) {
if (ctl.ID.StartsWith("X_")) {
// do stuff
}
}

but this gives a run-time error on the second line of "Object reference
not set to an instance of an object" which confuses me.

Surely the foreach loop should ensure that ctl is always set to an
object?

The ID property is a string, so that shouldn't be causing the problem.
If the control doesn't have an ID (if that's possible), the ID should be
"" and the StartsWith() method should return false.

<pause>
I just found something even more weird!! I changed the code to...

foreach (Control ctl in Page.Controls) {
string ctlId = ctl.ID;
Trace.Warn("ctlId = @" + ctlId + "@");
if (ctlId != "") {
if (ctlId.StartsWith("X_")) { // RUN-TIME ERROR HERE
// do stuff
}
}
}

and it bombed out on the same line, even though the tracing shows that
the ID was "". I don't understand how it *got* to that line when the
previous lines checks if the ID is "". Either I've done something
blindingly stupid here (not unlikely!!), or something very weird is
happening.

So, anyone able to explain to me what's going on here? TIA.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
2 1601
I believe you have at least one Control with a null id. Unfortunately, it is
not an error to concatenate a string with a null string. So:

Trace.Warn("ctlId = @" + ctlId + "@");

will not throw an exception. Neither will:

if (ctlId != "")

If ctlId is null, it will evaluate to true (ctlId is not equal to an empty
string).

Also, you should be awaare that the Page.Controls Collection is only a
Collection of those Controls immediately under the Page. You would need a
recursive function to get at all the nested Controls in the Page.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Alan Silver" <al*********@nospam.thanx> wrote in message
news:uc**************@nospamthankyou.spam...
Hello,

I have code like the following...

foreach (Control ctl in Page.Controls) {
if (ctl.ID.StartsWith("X_")) {
// do stuff
}
}

but this gives a run-time error on the second line of "Object reference
not set to an instance of an object" which confuses me.

Surely the foreach loop should ensure that ctl is always set to an
object?

The ID property is a string, so that shouldn't be causing the problem.
If the control doesn't have an ID (if that's possible), the ID should be
"" and the StartsWith() method should return false.

<pause>
I just found something even more weird!! I changed the code to...

foreach (Control ctl in Page.Controls) {
string ctlId = ctl.ID;
Trace.Warn("ctlId = @" + ctlId + "@");
if (ctlId != "") {
if (ctlId.StartsWith("X_")) { // RUN-TIME ERROR HERE
// do stuff
}
}
}

and it bombed out on the same line, even though the tracing shows that
the ID was "". I don't understand how it *got* to that line when the
previous lines checks if the ID is "". Either I've done something
blindingly stupid here (not unlikely!!), or something very weird is
happening.

So, anyone able to explain to me what's going on here? TIA.

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #2
>I believe you have at least one Control with a null id.

How can the ID be null? If the control itself is not null, and the ID is
a string (according to the SDK), then surely the ID should give you an
empty string. Please explain how it can be null.

I changed my code to...

if ((ctlId != null) && (ctlId.StartsWith("X_"))) {

.... and it works fine now. So you were absolutely correct, although I
still don't really understand how the situation could arise.
Unfortunately, it is
not an error to concatenate a string with a null string. So:

Trace.Warn("ctlId = @" + ctlId + "@");

will not throw an exception. Neither will:

if (ctlId != "")

If ctlId is null, it will evaluate to true (ctlId is not equal to an empty
string).
Hmm, that's really stupid. Still forewarned is forearmed as they say ;-)
Also, you should be awaare that the Page.Controls Collection is only a
Collection of those Controls immediately under the Page. You would need a
recursive function to get at all the nested Controls in the Page.


Ha, here's me thinking that it's not an issue in this case, and feeling
smug that I didn't make *that* mistake, when I realised that the
controls I wanted were all inside a placeholder, and so didn't show up
in the loop I coded. I needed to loop over the placeholder's control
collection.

Ho hum. Thanks for the warning there, you saved me hours of debugging!!

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #3

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

Similar topics

11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
6
by: Robert Werner | last post by:
I want to write one general purpose set of code that will handle the population (where required) of all controls on a form. Though I'm new to C# my instinct is to somehow hook into the Load event...
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
3
by: Sky Sigal | last post by:
I coming unglued... really need some help. 3 days chasing my tail all over MSDN's documentation ...and I'm getting nowhere. I have a problem with TypeConverters and storage of expandableobjects...
7
by: charliewest | last post by:
Hello - I'm using a Repeater control to render information in a very customized grid-like table. The Repeater control is binded to a DataSet with several records of information. Within the...
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, I have web user control (c# code-behind) and i'm trying to create a property that will provide the user with a drop down list of values inside the property box for that web user control...
2
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
3
by: shapper | last post by:
Hello, I have the following loop and error message: foreach (Theme theme in Profile.Collaborator.Themes) { ... And I get the following error: Object reference not set to an instance of an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.