Reputation: 4319
I have a WCF service which should return a maximum log date for a particular machine from a range, or return null if there is no log entry for that machine:
public DateTime? GetLastBootEvent(string laptopName)
{
ITDashboardDataContext itdb = new ITDashboardDataContext();
DateTime? latestEvent = (from be in itdb.tl_sta_bootTimes
where be.machineName.ToUpper() == laptopName.ToUpper()
select be.timestamp
).Max();
return latestEvent;
}
However, when I run it I get the following error:
"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs."
There's not much info there. I thought by using DateTime?
instead of DateTime
this should allow nulls to be returned?
I could handle this by the method returning some random date in the past, like MinDate, but I want to do this cleanly.
Upvotes: 0
Views: 420
Reputation: 664
I agree with wiero, it looks like some exception was thrown during the execution. To get exception on client side you can turn on exceptions transferring using IncludeExceptionDetailInFaults parameter in web.config (look at Turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server for example; however it will be better to turn it off on production).
If you get this exception details it will be easier to understand where the problem is.
Upvotes: 0
Reputation: 20230
You need to cast the timestamp within the query to a nullable datetime i.e.
DateTime? latestEvent = (from be in itdb.tl_sta_bootTimes
where be.machineName.ToUpper() == laptopName.ToUpper()
select (DateTime?)be.timestamp
).Max();
Upvotes: 1
Reputation: 82136
Sounds like you want to use DefaultIfEmpty here e.g.
DateTime? latestEvent = (from be in itdb.tl_sta_bootTimes
where be.machineName.ToUpper() == laptopName.ToUpper()
select be.timestamp
).DefaultIfEmpty(null).Max();
If there are no records Max will throw an exception without it.
Upvotes: 1