Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DataGrid template column and null pointer

Posted on 2005-05-08
3
Medium Priority
?
223 Views
Last Modified: 2011-09-20
Hi,

I was point to the following solution for use when binding a grid to an array list of objects. The objects as you can see people have other types that need to be navigated to get properties. However sometimes these properties are null and when they are my aspx page blows up with an: Object reference not set to an instance of an object.  Specifically when a Person does not have an Employer Object. How can I work around this? I wasn't sure how to best script for this. I am not sure really of the nature of the syntax below like the #  

This is C#

I tried something simple like this but it wasn't right and would not compile.
<% if (((Person)Container.DataItem).Employer != null )
{
      # ((Person)Container.DataItem).Employer.CompanyName
}
else
{
}
%>


<asp:TemplateColumn>
<HeaderTemplate>
      Lead Company
</HeaderTemplate>
<ItemTemplate>
      <%# ((Person)Container.DataItem).Employer.CompanyName%>
</ItemTemplate>
</asp:TemplateColumn>
0
Comment
Question by:JerryRodgers
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
laotzi2000 earned 500 total points
ID: 13956207
How about this?

<asp:TemplateColumn>
<HeaderTemplate>
     Lead Company
</HeaderTemplate>
<ItemTemplate>
      <%# ((Person)Container.DataItem).Employer==null ? "" : ((Person)Container.DataItem).Employer.CompanyName%>
</ItemTemplate>
</asp:TemplateColumn>
0
 

Author Comment

by:JerryRodgers
ID: 13956250
I LOVE this site!!! Worked like a charm.  

If you don't mind would you give me the 10 second explanation about the # after the <% %> I assume the <% are like the old scripting on/off flags and that I could then code in C# within those tags. by not sure why that failed me when I went with that approach.

Thanks,
Jerry
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13956274
I don't know how to explain.
<%# is different from <%
<%# is used in a item template, something like for each item do something.
Container.DataItem is only valid inside <%#
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question