# Wednesday, 01 May 2013

All of the applications I put into production use ELMAH to track unhandled exceptions.  In one our new releases of an application I noticed a semi-rare bug occurring through these logs.

Validation Failed

Validation Failed

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities.

I initially figured something extraordinary occurred for this to happen. Then it happened again and then another time. Now I felt it was time to give this error a more thorough review.  Something less then extraordinary was likely happening.

I use data annotations on my database model since I’m using Entity Framework Code First (EF CF) and in my view models.  I was a little concerned that some nefarious input was being entered that the client side unobtrusive JavaScript was missing as well as in my server side checks.

As it has almost always turned out when I suspect something like this, the true answer is something much simpler and sometimes even a bit embarrassing.

Here’s the offending view model / with the EF CF model.  Can you catch the issue?

//View Model
public class MyCourseViewModel
    [Display(Prompt = "Yada Yada Yada College")]
    public string College{ get; set; }
    [Display(Name = "Course Title", Prompt = "Intro to Psychology")]
    public string Name { get; set; }
    [Display(Name = "Course Prefix & Number", Prompt = "PSY 101")]
    public string CourseNumber{ get; set; }
//…more view model fields…. } //Database Model... public class MyDatabaseModel{ [StringLength(150)] public string College{ get; set; } [Required] [StringLength(150)] public string Name{ get; set; } [Required] [StringLength(25)] public string CourseNumber { get; set; } }

Mismatched Annotations

In my database model I set maximum lengths for my strings but I didn’t for my view model.  Enter the case where a user enters a value longer than the allowable amount and you have the client side & server side checks all pass on the view model but then an error occurs at the database model level since you’ve exceeded your length.


Make sure you at least have matched or created a more stringent rule in your view models as you do in your database models.  A simple oversight on my part and definitely not intentional.


Takeaway number two: Log, log, and log.  Then be sure to review those logs not just ignore them.  Take action when you begin to see a pattern.

Dealing with exceptions

Adding ELMAH to my MVC projects has been a breeze and has offered a great amount of insight into unforeseen bugs.  I often don’t react to the first new bug that ELMAH reports.  I instead look to see if it’s a pattern and when one emerges I then put it on my list to deal with.

Happy Coding! Smile

PS – I realize a better test setup would have been able to prevent this bug from ever making it into the wild. A unit test would have even missed this but not an integration test. Some extra food for thought on how testing can save your bacon…. Mmm… bacon.

Posted 05.01.2013  #    Comments [0]  | 
# Thursday, 14 March 2013


Location is everything in a lot of things.  The same is true of DateTime’s IsDaylightSavingTime method. This method uses the time zone setting of the machine it’s running on to determine this value.

What if you need to change that?


Be sure to read the comment on the answer!

The Tale

Duh, of course it does! My words after I realized my mistake!  You see, I live in Arizona and we don’t deal with the silliness of jumping forward and springing back our clocks. 

Old Code / Working Code

I was asked to update a project that was very small but because it was so old it wasn’t using the proper security constructs my employer had put in place (after this system was written) and it also was written long before the age of mobile and as such looked and worked poorly on all those shiny mobile devices out there.

In the project was a curious bit of code that I instantly wanted to destroy, I mean refactor:

    Private Function retTimeZoneAdjuster() As Byte

        Select Case Year(Now)
            Case Is = 2005
                If Now > #4/3/2005 2:00:00 AM# And Now < #10/30/2005 2:00:00 AM# Then
                    Return 2
                    Return 1
                End If
            Case Is = 2006
                If Now > #4/2/2006 2:00:00 AM# And Now < #10/29/2006 2:00:00 AM# Then
                    Return 2
                    Return 1
                End If
            Case Is = 2007
                    If Now > #3/11/2007 2:00:00 AM# And Now < #11/4/2007 2:00:00 AM# Then
                        Return 2
                        Return 1
                    End If
            Case Is = 2008
                    If Now > #3/9/2008 2:00:00 AM# And Now < #11/2/2008 2:00:00 AM# Then
                        Return 2
                        Return 1
                    End If

And it continued on through for several more years. The app has been around a while. The calling code was using this to add 1 or 2 hours to a very important timestamp.

I thought .Net must have something for this and I found DateTime’s IsDaylightSavingTime method.

Very Proud

I felt extremely accomplished when I turned the long statement into this concise bit:

        private DateTime Timestamp()
            var stamp = DateTime.Now;
            return stamp.AddHours(stamp.IsDaylightSavingTime() ? 2 : 1);

And for quite a while the rewritten program worked without issue.

Trouble In Pride Land

This last Sunday at 2AM we sprung forward and my happy little program and my happy group of users became, well, not so happy.

The email alerting me to the issue, since this was such a small system that we, I didn’t bother to put in logging capabilities to track the dreaded unhandled exception was very simple indeed. You could imagine my surprise. "WAT!!!!????  No longer working at all you say????  WAT!!!!?????  I haven’t touched that system since we launched it WITHOUT ISSUE".

Tests you ask? Oh, I think you know the answer to that.  This is a small project, what do I need to test I can hear myself saying.

I toiled on the ash heap for a bit and then it struck me.  We just changed Daylight Savings Time (DST). Could that be it?

Of course I opened up my editor and…


Duh, of course it does! I paused for a moment before I realized I live in Arizona and so does my server and we don’t do the DST shuffle. Ooooppps!

If I live in Arizona then why do I have to deal with this? Well, this timestamp is passed around to another server not in Arizona. 

And boom!


The Fix

The DateTime class will always use the local system’s time zone setting.  You can’t override this. That’s where the TimeZoneInfo class comes in.  With this class you can create a time zone and convert times with it and call IsDaylightSavingTime for that zone.

        private DateTime Timestamp()
            var mountainTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
            var mountainTime = TimeZoneInfo.ConvertTime(DateTime.Now, mountainTimeZone);

            return DateTime.Now.AddHours(mountainTimeZone.IsDaylightSavingTime(mountainTime) ? 2 : 1);

First, create a time zone that respects Daylight Savings Time (DST). Then convert the existing time (in my zone) to the new zone and then test if the time is DST in the new zone.

Hopefully, this will help someone who needs to be concerned about DST.  At least, it seemed like a nice little tale to tell.

Happy Coding! Smile

Posted 03.14.2013  #    Comments [0]  | 
# Thursday, 11 October 2012

Have you heard about the Succinctly Series? Syncfusion has produced a number of free e-Books on various subjects ranging from source control to jQuery to HTTP.  I highly recommend you pick up these free books. You’ll have to register but it’s worth it. This is an unsolicited review. Smile

So far I’ve read the Knockout.js Succintly, ASP.NET MVC 4 Mobile Websites Succinctly, PDF Succinctly and JavaScript Succinctly. They are great beginner books and very quick reads especially if you are beyond the basic level. They average around 60 pages per book with 150 pages being the biggest I’ve seen.

They seem to be rolling out a new book every few months. So far they have:

Knockout.js Succinctly

C++ Succinctly

ASP.NET MVC 4 Mobile Websites Succinctly

LightSwitch Succinctly

Javascript Succinctly

HTTP Succinctly

PDF Succinctly

Git Succinctly

jQuery Succinctly

Happy Coding! Smile

Books | Git | JavaScript | jQuery | Knockout | review
Posted 10.11.2012  #    Comments [0]  |