DateTime #13

Closed
Autoclimate opened this Issue Feb 27, 2011 · 2 comments

Projects

None yet

3 participants

@ghost
ghost commented Feb 27, 2011

Hi,

With this code:

var table = new tblResource();
dynamic resources = table.All();

and:

@foreach(var r in resources){
   <tr>
        <td>@r.ResourceId</td>
        <td>@r.DateAdded.ToString("dd-MM-yyyy")</td>
        <td>@r.Overview</td>
        <td></td>
        <td></td>
    </tr>
}

I get an error message as follows:

The best overloaded method match for 'System.DBNull.ToString(System.IFormatProvider)' has some invalid arguments.

I am completely new to dynamics, Web Matrix and Massive but should Massive have picked up that this is a DateTime object or is there something further I need to know/do.

Thanks in advance

It looks like your DateAdded column allows NULL values. In a full ORM, you could type it as a DateTime? but Massive will just return the pure value, in this case DBNull. Thus, you will probably need some sort of function that converts DBNulls to null and then test for nulls before doing a .ToString() (it would be ideal if Massive took care of DBNull => null conversions for you). Also, it cannot be an extension method because dynamics do not support direct extension methods. I believe Massive will return the date as a DateTime if there's a value in it.

In my own code, I created a mapper class that maps the dynamic onto my POCO class. You could also use AutoMapper, which is what I will probably end up doing. This avoids the issues that may crop up directly dealing with dynamics.

If you want to directly deal with your issue for now, just do:

@if (r.DateAdded != System.DBNull) {
    <td>@r.DateAdded.ToString("dd-MM-yyyy")</td>
} else {
    <td>Unknown</td>
}
Contributor

Yep - Massive will only know what type it's using when ADO hands over the value. It looks like this is an "immediate" thing.

Two things come to mind: I need to do some work RE DBNull and ADO really should be returning DataTime?

@robconery robconery closed this Apr 29, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment