Issue with displaying foreign keys with null values

0 views
Skip to first unread message

Robin

unread,
Jun 10, 2009, 11:47:09 AM6/10/09
to mvccontrib-discuss
Hi,

I'm having an issue with displaying foreign key values that are null.

I have a Scholar table with foreign key field "CountryID" referencing
the Country table.

I am then adding the column into my Grid as such:

<%= Html.Grid<Models.Scholar>(Model).Columns(column =>
{
column.For(x => x.Id);
.
.
column.For(x => x.Country.Title);
.
.
column.For(x => Html.ActionLink("Details", "Details", new { id
= x.Id })).DoNotEncode();
}).Empty("There are no scholars")
%>

However not all scholars have a country, so the CountryID could be
null.

When I try to view the table I get a "System.NullReferenceException
was unhandled by user code" error on line 162 of GridColumn.cs:

var value = _columnValueFunc(instance);

At this point I get a bit lost as the code is beyond me. As far as I
can tell _columnValueFunc is essentially a Compiled Linq expression as
I am using Linq To Sql. And the issue is that the linq expression
contains a null reference as x.Country is null then x.Country.Title
will be the null reference. That's my understanding.

With my limited grasp of Linq and MvcContrib.Grid I don't know whether
this is a bug in the Grid or LinqToSql or the code generated by
LinqToSql in my mvc project but my solution is to add a try-catch
statement around the problematic code in GridColumn.cs.

If I am doing something wrong I would appreciate some pointers in the
right direction or if this is indeed a bug in the Grid, I can raise an
issue if you want.

Thanks for any help and thanks for this great tool.

Regards,
Robin





Jeremy Skinner

unread,
Jun 10, 2009, 12:37:35 PM6/10/09
to mvccontri...@googlegroups.com
Hi Robin,

You are correct - _columnValueFunc is a delegate that invokes the lambda expression to return the value. If Country is null, then a NullReferenceException will occur as the Title property cannot be accessed. 

I'm in two minds as to what the Grid should do in this situation. As it stands at the moment, null checking is the responsibility of the developer. However, I could change it to wrap the delegate invocation in a try-catch and silently return null if a NullReferenceException occurs. The downside here is that it would make debugging problems more difficult.

Anyway, for now putting a null-check inside the lambda should solve the problem:

column.For(x => x.Country == null ? null : x.Country.Title);

Jeremy

2009/6/10 Robin <robin....@minervation.com>

Robin

unread,
Jun 11, 2009, 2:59:53 AM6/11/09
to mvccontrib-discuss
Hi Jeremy,

Thanks for the quick response. I've decided to use your suggestion
and do the null check before I pass in the lambda. I think you are
right in saying this should be the responsibility of the developer.

Thanks again,
Robin

On Jun 10, 7:37 pm, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
> Hi Robin,
> You are correct - _columnValueFunc is a delegate that invokes the lambda
> expression to return the value. If Country is null, then a
> NullReferenceException will occur as the Title property cannot be accessed.
>
> I'm in two minds as to what the Grid should do in this situation. As it
> stands at the moment, null checking is the responsibility of the developer.
> However, I could change it to wrap the delegate invocation in a try-catch
> and silently return null if a NullReferenceException occurs. The downside
> here is that it would make debugging problems more difficult.
>
> Anyway, for now putting a null-check inside the lambda should solve the
> problem:
>
> column.For(x => x.Country == null ? null : x.Country.Title);
>
> Jeremy
>
> 2009/6/10 Robin <robin.mass...@minervation.com>
Reply all
Reply to author
Forward
0 new messages