views:

602

answers:

1

Normally if I'm linking an ObjectDataSource to a GridView and I have a TemplateColumn that has an Eval in it and it's Null, I can just put a ".ToString()" it works fine. For some reason, this doesn't work the same when you're using Linq to SQL.

I originally was using XSD files for my DAL with a custom BLL. I tied it to the GridView with an ObjectDataSource. I'm in the middle of swapping out the XSD files with Linq to SQL and everything is working just like the old way except for the columns that can have Null values.

Has anyone run into this before and if so, how do I work around this problem?

+2  A: 

Most everything that LINQ returns is of Nullable types. So in your binding expressions you need to use GetValueOrDefault().ToString() or the new "??" null coalescing operator rather than just plain old ToString(). I hope this helps. Check this link out to.

Example:

// this will output the int if not null otherwise an empty string.
<%# (int?)Eval("MyIntegerField") ?? "" %>
James
Wow - interesting! thanks!!
EdenMachine
VB: If(DirectCast(Eval("MyIntegerField"), System.Nullable(Of Integer)), "")
EdenMachine
Now I know why I stopped using VB so many years ago!
James