Hi,
We are having some trouble serializing a WebDatePicker on a server and deserializing it on another one where Culture is different (or even if it is the same server but Culture used on deserialization depends on the web client).
Desrialization fails with "String was not recognized as a valid DateTime."
Is this the expected behavior?
Thanks.
Here is how you can reproduce this issue:
protected void Page_Load(object sender, EventArgs e)
{ if (!Page.IsPostBack) { PersistenceManager lManager = PersistenceManager.GetInstance(); MemoryStream lSTream = (MemoryStream)GetDatePicker(); lSTream.Position = 0; Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("es-UY"); PersistenceData lData = PersistenceData.FromStream(lSTream); var lControl = new WebDatePicker(); lManager.LoadControl(lControl, lData.Controls[0]); PlaceHolder1.Controls.Add(lControl);
} }
public Stream GetDatePicker(){ WebDatePicker lDatePicker = new WebDatePicker(); lDatePicker.BackColor = System.Drawing.Color.Red; lDatePicker.Value = new DateTime(2012, 12, 21);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
PersistenceManager lManager = PersistenceManager.GetInstance(); PersistenceData lData = new PersistenceData(); lData.Controls.Add(lManager.SaveControl(lDatePicker, "control")); return lData.ToStream(); }
Hello Ricardo,This issue you are facing is not specific to our controls – it is general programming issue. You can find discussions regarding it on the following links:http://stackoverflow.com/questions/8182895/how-to-serialize-deserialize-a-double-value-independent-of-culture
http://stackoverflow.com/questions/4876198/what-is-the-right-approach-for-serializing-deserializing-data-in-a-culture-insen
http://stackoverflow.com/questions/3231386/when-should-i-specify-currentculture-or-invariantculture-and-when-should-i-leave
I am familiar with the issue but I do not have control over this serialization as I am doing it using Persistence Framework save and load functions.
Isn't Persistence Framework expected to work under this circumstances? The Date that is being serialized is that of the WebDatePicker.
I set to bold to the part of the code that uses PersistenceFramework in the previous post, just in case it wasn't clear enough.
Thanks
Hello Ricky,No Persistanence is expected only to keep the data as it was given. In this case you will have to for example every time before saving date/double to cast it in en-US and to use Datetime.Parse method - http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
But that is not what persistence is doing, I am not giving it a culture dependent representation of a date, I am giving it a DateTime which works on any culture. The string representation is introduced by the Persistence not me.
What I need is exactly what you say, that Persistence framework keeps data as I give it to it.
Even more, I don't think this a rare scenario, imagine a website where you allow user's culture to be used, you wouldn't be able lo see a persisted page if it was persisted from a user in another culture? Not if it includes a WebDatePicker? which, by the way, adapts to current culture...