Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi frnds,
I want to change the format of date field to any desired format after filling the data into a DataSet. Anyone can say a simple way to achieve this.

C#
mySQLConnection = new SqlConnection(S"Data Source=(local);Initial Catalog=myDb;User Id=user;Password=pw;");
mySQLConnection->Open();
myDataSet = new DataSet("Sample");

myDataAdapter = new SqlDataAdapter(S"select * from SampleTable",mySQLConnection);
myDataAdapter->Fill (myDataSet,"SampleTable");

dt = myDataSet->Tables->Item["SampleTable"];

dt->Columns->get_Item("DateField")->Format("mm/dd/yyyy hh:mi:ss") // this is not availabe now


Is there any idea to change the format in this way?? or any other way without having an iteration through all fields??
Posted
Comments
Sinisa Hajnal 19-Nov-14 2:36am    
Is this date field or string? If it is a string then you have no choice but to iterate through and change each one OR format it before it fills the dataset. If it is a date, leave it as is and format it just in time for presentation layer (that is, when you're showing it to the user.


In general, holding dates as string is TERRIBLE design that should be changed as soon as possible.
Member 11168418 19-Nov-14 2:45am    
This is date field actually. I want to save it to an xml (say, sample.xml) file, but before that the format should be changed. How can i achieve this.?
When i use, myDataSet->WriteXml("sample.xml");, the date is displayed like, <datetime>2014-04-22T09:37:18+05:30. But i want to display it in other format too like mm/dd/yyy hh:mi:ss or dd/mm/yyyy hh:mi:ss. I am not changing the data type to String, it resides as DateTime.
Sinisa Hajnal 19-Nov-14 3:55am    
Hmmm...options
1. Write your own WriteXML? Then you can change the data as you see fit.
2. Add new column to the dataset and return string in suitable format from the datasource.
3. Add new computed column to the dataset in your code - with computed being formatted string
4. Not sure if there are properties to affect output format on the dataset column, try it.

I cannot help you more unless I re-create the situation and play with it (sorry, no time for that) :(
Member 11168418 19-Nov-14 4:04am    
ok, thats fine. Thank you for ur information. Let me try these options. :)
Sinisa Hajnal 19-Nov-14 4:09am    
Try 2 first, it is the fastest one and simplest. Then 3.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900