September 14, 2012

Date Problem when having Client & Server in different TimeZone

Hi, I want to share my problem and its solution i found..

Problem : I have developed one client Application in which iam getting data from database in to DataView Object, Now my task is to show the records on the Grid which are greater than or equal to current system date.
To do so i used following code
(overview not exactly)
 
String today = DateTime.Now.ToString();
dv.RowFilter = string.Format("TODATE >=#{0}#", today);
 
Its working fine on some systems and raising exceptions on some other systems.
Solution : the exact problem is culture the system. When the culture is different what we expecting then its getting execption to fix this problem i coded the above code as follows:
 
CultureInfo culture = new CultureInfo("en-US");
String today = DateTime.Now.ToString("d",culture);
dv.RowFilter = string.Format("TODATE >=#{0}#", today);
 
Now this code perfectly works on any ssytem with any culture.
Happy to Share Code
 

No comments:

Post a Comment