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

Help with Errors

P: n/a
I'm using the following two segments of code on ASPX pages with Datalists.

The following segment of code produces the error "Object reference not set
to an instance of an object."

Label lblPhone = (Label)(e.Item.FindControl("lblPhone"));
if (lblPhone.Text.Length > 0)
lblPhone.Text = string.Format("{0:(###)###-####}",
double.Parse(lblPhone.Text));

However, this segment of code works fine.

Label lblPhone = (Label)(e.Item.FindControl("lblClubPhone"));
if (lblPhone.Text.Length > 0)
lblPhone.Text = string.Format("{0:(###)###-####}",
double.Parse(lblPhone.Text));

Different pages same code same Datalist settings, same label properties.
I've even copied the label id's from the html into the code to make sure I
was not missing something there.

Does anyone have any ideas???
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Hi CDWaddell,

Thank you for posting in the community!

Based on my understanding, you use 2 code segments on different pages, but
one generates error, while the other works well.

===============================================
As you said, "Different pages same code same Datalist settings, same label
properties."
Actually, as I review, the 2 code segments is not the same. The 2
parameters for FindControl methods are not the same:
e.Item.FindControl("lblPhone")) e.Item.FindControl("lblClubPhone")

I think your Label control's id should be "lblClubPhone" not the
"lblPhone".
"Object reference not set to an instance of an object." error generates
because "Label lblPhone = (Label)(e.Item.FindControl("lblPhone"))" is a
null reference.

Normally, it is a good programming practice to check whether the reference
is null before using it.

===============================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #2

P: n/a
Jeffrey,

Actually I should have stated that the page objects are named lblPhone
on page A and lblClubPhone on page B. Other than the object ID's
everything is identical.

Thanks,
Cary

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3

P: n/a

Hi Cary,

Thanks for your feedback.

Based on your feedback, you did not write the wrong parameter for the
FindControl method.

At this point, I think the information you provided is not enough for find
out the problem. Can you provide the more detailed code and other
information?

Which code does this exception generate? If it generate in the code you
pasted, I think the FindControl method just returns a null reference. (You
can determine this using code)

I will wait for your further feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4

P: n/a

Hi Cary,

Does my reply make sense to you?
If you have the further information, please feel free to post, I will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.