Print Page | Close Window

Handling .net DateTime Fields

Printed From: Crystal Reports Book
Category: Crystal Reports for Visual Studio 2005 and Newer
Forum Name: Data Connectivity
Forum Discription: How to connect to data sources and export reports
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=1003
Printed Date: 01 May 2025 at 4:25pm


Topic: Handling .net DateTime Fields
Posted By: Craig Adams
Subject: Handling .net DateTime Fields
Date Posted: 13 Jul 2007 at 1:56pm
Hi Everyone,
 
I have been using datasets as a datasource for my crystal reports and when I encounter a dbnull in a date field, crystal reports correctly formats it for me.
 
I have recently begun to use .net classes as a datasource.  When I have a date field defined as system.DateTime and it is not initialized ( ie it is equal to DateTime.MinValue, crystal reports will display an invalid date. It displays the DateTime.MinValue instead of displaying a blank date as I would like it too.
 
Has anyone dealt with this situation before?  I would not want to go back to datasets.  I could add addtional fields to my class that would handle this problem but they would only exist for my crystal report.  Sounds like a hack to me.
 
I would appreciate anyone's feedback on this issue.
 
Craig Adams
Vivus Software, Inc.



Replies:
Posted By: jkwrpc
Date Posted: 15 Jul 2007 at 3:23pm
It would seem to me that if your datasource is being handled through .Net classes which are objects, you will need to return a proper date object in the same manner. I am not sure that is a hack since it relflects your overall data design. It may take too long to have you explain what your classes return. In my case I often return a dataset based on some business logic. In that situation  it is a simple matter to deal with correcting the date issue in the .Net code.
 
Another thought might be to create a CR formula to deal with the date value returned that you have identified as being in error. 
 
Regards
 
John W.
http://www.CustomReportWriters.net - www.CustomReportWriters.net


Posted By: Craig Adams
Date Posted: 16 Jul 2007 at 8:06am
Hi John,
Thanks for your reply.  My complaint is that Crystal Reports does not seem to understand the DateTime type in .net.  An uninitialized date field in .net is not seen that way in Crystal Reports but gets displayed as a non-sensical date.
 
I have coded around this by adding a public property which is simply a string.  Inside my get statement, I simply test to see if the date is unitialized and then return an empty string. If the date is valid, I convert the date to a string. 
 
The above is working for me but it means that for each date in my class, I must add an additional property to format it for Crystal Reports. This property is used exclusively for crystal reports.
 
As I said in my post, when using datasets, null dates are handled nicely by Crystal Reports.
 
Thanks again for your feedback,
Craig Adams
 
Vivus Software


Posted By: jkwrpc
Date Posted: 16 Jul 2007 at 8:30am
Craig:
 
Thanks for sharing the solution.  I believe I better understand your issue with CR from the last post.  Under the circumstances creating the property, albeit just for CR, may be the most effective solution.
I am curious, when you say the date is non-sensical, is it without a pattern? Again my simple train of thought is whether or not there is a way to have a CR formula filter for a valid date, such as within a date range , etc.
 
Regards,
 
John W.
http://www.CustomReportWriters.net - www.CustomReportWriters.net


Posted By: Craig Adams
Date Posted: 16 Jul 2007 at 11:02am
John,
The nonsensical date I am referring to is the default .net date when a date variable is not initialized. ie 1/1/0001. 
 
I initially tried a formula in the crystal report but could not get anything to work with that date.  I hate hard coding anything because things can change in the future.  At least in my class I can check for Date.MinValue to know whether the date has been initialized or when using a nullable date, Date.HasValue 
 
All the best,
Craig


Posted By: jkwrpc
Date Posted: 16 Jul 2007 at 12:20pm

I suppose you have tried this, but since the date is not initialized, I assume that means it 'is nothing'.  Have your tried writing the CR formula in Basic and then test for  '{datefield} is nothing' using some kind of control logic. I have used this a number of times where I am passing a dataset that may not have been initialized. So I test whether it exists first.

I have never tried this in CR so am clueless if it works. It does seem since it is not initialized that the date object is nothing. Now if CR formula would support that logic it might be a way to move it to the report and out of the application....
 
Regards,
 
John W.
http://www.CustomReportWriters.net - www.CustomReportWriters.net  


Posted By: Craig Adams
Date Posted: 16 Jul 2007 at 12:34pm
John,
Thanks for the suggestion but the problem lies in the fact that the date object is a valid date.  If it was nothing then I could do a test for that in a forumula. 
 
DotNet is a pain the way it handles uninitialized dates.  That is why in the 2.0 framework, the Nullable(date) type is very valuable.  Unfortunately, it only is of value inside a .net program and Crystal Reports does not have equivalent functionality.
 
I think I will be content with my solution for now unless someone has a more elegant solution.
 
Thanks for your suggestions.
 
Craig


Posted By: jkwrpc
Date Posted: 16 Jul 2007 at 2:15pm
Craig:
 
One parting thought since I dont know what CR stuff you have tried and you likely have already given this logic a go...but,
 
As I understand date.minvalue returns something like 1/1/0001  which is the .Net minimum date value.
 
CR however will not see a  valid date value less than year 100 or 1/1/0100 so it likely does not recognize the minvalue as a valid date being returned, hence your need to create an empty string with the property. It seems you should be able to do the same thing in some way by testing the value in the report as to whether or not it is a valid CR date.
 
So if you tested to see if the date was a date in CR in a formula such as follows (the CR IsDate function is a boolean) 
--------------
IsDate({datefield}) and it was your non-sensical date it would return false.
 
I am assuming you want to ignore the non-sensical date which you may not want to do... so you could adjust accordingly. You may also need to adjust for the difference in date minimums year 0001 in .Net and year 100 in CR)
continuing----
 
If you coupled it with a null test
 
If IsDate({datefield}) <> false then ....
If {datefield} not null then ....
----------------------------------
 
Does this get you close to where you want to be with what is being returned and having the logic in the report?
 
Now I will go back to sleep and leave you alone.
 
Regards,
 
 
John W.
http://www.CustomReportWriters.net - www.CustomReportWriters.net
 
 


Posted By: andyoneill
Date Posted: 18 Sep 2007 at 7:15am
Can you not just return System.DBNull.Value when the date is mindate, from the get?



Print Page | Close Window