I am working on a project and i am not having any database. I am using Dataset of ADO.Net and then to store my data i write the xml file and read xml file and load the dataset again. The xml file is the project file and i have to move this file from system to system.
In this data set, there also a column of DateTime.
I want to store Date in field in a specific format ( dd/MM/yyyy ).
Currently, my problem is that, it detects the system’s date format and read the date. But system’s date format can vary system to system. Some people use MM/dd/yyyy and some use dd/MM/yyyy. So, when the format is changed, it throws the exceptions. Is their any solution for it?
Can i force DataTime field of data table to accept date in my custom format?
Thanks in advance.
I’ve had similar issues with serializing date time in the past.
I’d recommend that you drop your datetime field and use a string instead. That way you have full control over what format the date time object is entered into your DataSet.
Once you have it entered in as a string, you need to record what culture and what format the datetime is recorded in. You can either hard code those two values inside your software and hope they don’t change (not ideal). Drop those to a .config file (better).
or
You add those additional fields to your dataset.
So when you read your date from your DataSet to instantiate a DateTime object you then use.
*Note that fields inside [] are values retrieved from a row in the dataset.
This way you can use this datetime field on any machine as you are specifying in the dataset what culture and format provider to use.
You will need to refactor everywhere you are instanciating a datetime field from the dataset to use those datetime provider and culture information.
Personally, I’d go with reading the format and culture information from a .config file as these are not likely to change on a row by row basis in your dataset.