Xaml Databound property changing value in Child_Insert()

Xaml Databound property changing value in Child_Insert()

Old forum URL: forums.lhotka.net/forums/t/11068.aspx


gautamh posted on Wednesday, January 18, 2012

Hi,

I'm hoping someone can provide some insight into a strange issue I've been experiencing.

I have a silverlight grid bound to an editable collection. All columns on the grid are databound to csla properties.

I have a DateTime property. When the property is set using the control on the UI, the value is correct (for e.g., DateTime.Now). However, when I call save on the collection and the Child_Insert() is called for the item in the collection, the date property has changed value.

The property setter is as below. I have put in Debug statements to check the value before and after the setter and it shows as expected.

public static readonly PropertyInfo<DateTime> FundingDateProperty = RegisterProperty<DateTime>(c => c.FundingDate, null, new DateTime());
/// <summary>
/// Gets or sets the funding date.
/// </summary>
/// <value>The funding date.</value>
public DateTime FundingDate
{
    get { return GetProperty(FundingDateProperty); }
    set
    {
    try
    {
        string s = string.Format("Value of fundingdate is : {0}", value.ToString());
        System.Diagnostics.Debug.WriteLine(s);
    }
    catch { }

    SetProperty(FundingDateProperty, value);

    try
    {
        string s = string.Format("Value of fundingdate after set is : {0}",this.FundingDate.ToString());
        System.Diagnostics.Debug.WriteLine(s);
    }
    catch { }
    }
}

However, in the Child_Insert() method, when I output the value, the value has changed. For e.g., a FundingDate of 18/01/2012 becomes 17/01/2012.

try
            {
                string s = string.Format("Value of fundingdate at Insert() is : {0}", FundingDate.ToString());
                System.Diagnostics.Debug.WriteLine(s);
            }
            catch { }

To compound this, this is happenning only on some clients and not on any developer machines. So, we just cannot debug this.

The only other thing to note here is that there is a business rule to prevent the date being set to a date less than today.

This the business rule class.

private class FundingDateLessThanToday : Csla.Rules.BusinessRule
        {
            protected override void Execute(Csla.Rules.RuleContext context)
            {
                var target = (MusketeerFundingRequest)context.Target;
                var fundingDate = target.ReadProperty(FundingDateProperty);
                if (fundingDate.Date < DateTime.Now.Date)
                    context.AddErrorResult("Funding date cannot be less than today.");
            }
        }

 

Has anyone experienced this behaviour before in which property values are changed in the interval between setting them via databinding and saving them in the xxx_insert() method?

Any help will be greatly appreciated as I've come up against a total brick wall for the past two days.

Regards,

Gautam

 

 

JonnyBee replied on Wednesday, January 18, 2012

This is typically caused by  computers in different time zones and using DateTime in UTC format.

IE: Your client sets the DataTime as UTC (meaning the datetime at GMT +/- computers timezone).

When deserialized on the server at another timezone the datetime will be the "local" time in the servers timezone for the same timestamp and may be another date.

I  have also seen the same happening between machines when they are not properly synchronized on "summertime"/"wintertime" (and thus ends up on different timezones).

 

gautamh replied on Thursday, January 19, 2012

Hi JonnyBee,

I don't think this is the case. The datetime variable/field is set to only the Date part.

Also, all the machines concerned are on the internal LAN. So, they are synchronized. You mentioned deserialization. Why would the datetime change in when the value is deserialized? Surely, whatever value is there in the variable/field is serialized on the client and then just deserialized on the server?

Also, would you able to suggest where in the csla code can I put in some debugging information to check if indeed the value is being changed during deserialization?

Thanks.

JonnyBee replied on Thursday, January 19, 2012

Hi,

The may be syncronized and on the same LAN but may have different TimeZone settings on the computer !!

Read this article: Coding Best Practices Using DateTime in the .NET Framework especially section "The Special case of XML"

gautamh replied on Tuesday, January 31, 2012

It was indeed an issue with datetime serialization using wcf. I ended up using DateTime.SpecifyKind(value, DateTimeKind.Unspecified) to send the date.

Copyright (c) Marimer LLC