Help with Errors

  • Thread starter Thread starter CDWaddell
  • Start date Start date
C

CDWaddell

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???
 
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.
 
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
 
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.
 
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.
 
Back
Top