Databinding on Related Field

Posts   
 
    
pcoulter
User
Posts: 14
Joined: 11-Nov-2004
# Posted on: 02-Jan-2007 20:18:45   

I just upgraded my runtime libraries to the latest version, and I started getting a NullReferenceException. I'm using VS2005, ASP.NET 2.0, SQL Server 2005, and Adapter.

Here's the code that generates the exception:

<asp:TemplateField HeaderText="Carrier">
   <ItemTemplate>
      <%# ((ApplicationEntity)Container.DataItem).Carrier.Name %>
   </ItemTemplate>
</asp:TemplateField>

I'm assuming that a record has a null CarrierEntity, and thus the error. It's not that surprising to me that this is happening, except that this worked just fine before I upgraded. I went from version 2.0.0.60911 to version 2.0.0.61205 of the ORMSupportClasses. I am using a prefetch to load the related entity. I loaded the page once, and it worked. I then upgraded the runtime libraries, and it threw the error.

If this is a bug, it'd be great to get it fixed. If however, this is the intended behavior, then what is the recommedation when databinding to a related entity when it could be null? I'm currently binding directly to an EntityCollection, not to an LLBLGenDataSource2. Is the LLBLGenDataSource2 preferred over the EntityCollection? I can't check for null inside the <%# %> syntax, so should I be doing this in the code-behind? Any advice would be appreciated.

Thanks, Pete

[edit] This should probably be moved to the 'Generated code' forum. Sorry flushed .[/edit]

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 02-Jan-2007 21:17:27   

As with all exceptions: please post the full stacktrace. Without it, we don't know where to start.

Frans Bouma | Lead developer LLBLGen Pro
pcoulter
User
Posts: 14
Joined: 11-Nov-2004
# Posted on: 02-Jan-2007 21:28:14   

[NullReferenceException: Object reference not set to an instance of an object.] ASP.agents_applications_aspx.__DataBind__control13(Object sender, EventArgs e) in c:\Documents and Settings\PCoulter\my documents\visual studio 2005\projects\medicarepartd\MedicarePartD.Main\Agents\Applications.aspx:61 System.Web.UI.Control.OnDataBinding(EventArgs e) +99 System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +206 System.Web.UI.Control.DataBind() +12 System.Web.UI.Control.DataBindChildren() +216 System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +216 System.Web.UI.Control.DataBind() +12 System.Web.UI.Control.DataBindChildren() +216 System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +216 System.Web.UI.Control.DataBind() +12 System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +221 System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3004 System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +59 System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +11 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +111 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +29 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70 System.Web.UI.WebControls.GridView.DataBind() +4 Agents_Applications.BindApplications(DateTime startDate, DateTime endDate, Int32 agencyID, String agencyAssignedAgentID) in c:\Documents and Settings\PCoulter\my documents\visual studio 2005\projects\medicarepartd\MedicarePartD.Main\Agents\Applications.aspx.cs:72 Agents_Applications.btnSearch_Click(Object sender, EventArgs e) in c:\Documents and Settings\PCoulter\my documents\visual studio 2005\projects\medicarepartd\MedicarePartD.Main\Agents\Applications.aspx.cs:127 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 02-Jan-2007 21:45:45   

Well, if carrier is null, it's null, so the call will crash. so first of all, you've to determine if the related entity exists. (you did re-generate code as well?). If it does and it's not there, it's not loaded in. So could you enable tracing to see if the query is made?

Then, you can add code to your binding code: <%# if(Eval("Carrier")==null) { string.Empty; } else { ((ApplicationEntity)Eval("Carrier")).Name;} %>

THough I'd map a field on related field in the designer for this (so ApplicationEntity.CarrierName for example).

Frans Bouma | Lead developer LLBLGen Pro
pcoulter
User
Posts: 14
Joined: 11-Nov-2004
# Posted on: 02-Jan-2007 22:22:44   

Adding an if statement in the <%# %> doesn't compile. Just

<%# if (true) {"test";} %>

will give you a compilation error of "Invalid expression term 'if'".

Which query are you suggesting that I look for in the trace? The prefetch to the Carrier table?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 02-Jan-2007 23:16:20   

pcoulter wrote:

Adding an if statement in the <%# %> doesn't compile. Just

<%# if (true) {"test";} %>

will give you a compilation error of "Invalid expression term 'if'".

You're right, I was confused with a full expression I wrote for this forum:


... visible='<%# ((int)Eval("AmountOfAttachments") >0) || ( ((int)Eval("AmountOfAttachments")<=0) && (SessionAdapter.GetUserID()==(int)Eval("UserID")) && base.ForumAllowsAttachments && base.UserMayAddAttachments)%>'>

which actually evaluates to a value, I think that's the reason why this fails.

Though a field mapped onto a related field takes care of this, so you should try that first.

Which query are you suggesting that I look for in the trace? The prefetch to the Carrier table?

Yes. simple_smile

Frans Bouma | Lead developer LLBLGen Pro