Last post Oct 01, 2009 04:49 AM by sjnaughton
Sep 29, 2009 01:18 PM|nwalker28|LINK
I have the following function which checks to see if a job is not marked as won or lost and has a value for the won/lost date then throw an error saying that the job needs to be won or lost before a won lost date is recorded.
Here is my problem if the job is not marked won or lost and a user enters something in the won/lost date they cannot get out of the error. For some reason when the text box is completely cleared instead of passing in an empty or null string the value parameter
is equal to what it was before so the error keeps reoccurring. The reason it works when I'm not catching this error is because this OnJobWonLostDateChanging() is called twice and the second time through value is null and then is appropriately applied to the
database. I do not understand why when clearing the text box the value from before it was cleared is passed in. If I enter some other string then that is passed in correctly on the first try just not the empty or null string.
partial void OnJobWonLostDateChanging(DateTime? value)
if (this.JobWon == 1 && value.HasValue == true)
throw new ValidationException(
"Job Won/Lost Date cannot be entered until the job is marked won or lost");
Sep 30, 2009 03:49 AM|DaveRuss|LINK
I have a feeling that the OnxxxChanging events also fire during CONSTRUCTION of an instance, before the changes are applied - hence the reason you are seeing it fire twice. I thought there was something about it in Julia's EF book, but I can't find it for
the moment. Perhaps you will have more luck?
Sep 30, 2009 09:18 AM|nwalker28|LINK
I actually do not have a book on this subject. Which book are you referring to and what is the topic at hand? Is this LINQ to SQL or LINQ to Entities or something completely different? Knowing what to look for will greatly help me in my search.
Sep 30, 2009 09:24 AM|nwalker28|LINK
I agree that it probably is fired during construction of an instance but what I don't understand is if I change the string from one date say 10/15/2009 to 11/30/2010 that on the first and second call the value is correctly shown as 11/30/2009 in the debugger.
However if I change the date from 10/15/2009 to a null or empty string then on the first call this value is still 10/15/2009 and on the second call then the value is null. I want to call it an error in the framework but what are the chances?
Sep 30, 2009 09:35 AM|sjnaughton|LINK
I found that it is called three times and on the third call all properties were populated.
Linq to SQL
Sep 30, 2009 10:26 AM|nwalker28|LINK
So how can I make sure that my code only runs on the third time? Should I just throw another if(this.project != null) in front of my logic? Seems kind of sketchy...
Oct 01, 2009 04:49 AM|sjnaughton|LINK
It may be worth taking this to the
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/threads EF Forum and asking the question there as the issue is more L2S than DD, but please report the answer bak here in both threads [:)]