one is still in production the other is not. that is the biggest difference when it comes to getting one.
the D-40 is not really much of an upgrade from the D-50 if anything it is taking a step sideways or even a half step back.
If you are looking at upgrading think about the d-200 which should drop in price around christmas time, or the new d-300 which should be on shelves around the same time.