DateFormat Problem in ASP.NET
-
I have needed to insert and update DateTime Field in MSSQL Database Table. I am using Text Querry. My input Control is TextBox where the format of Date is "dd/MM/yyyy". In Database it should be save as "yyyy-MM-dd". I have used a Function to convert Date Format as: public static string ConvertToDate(string pstr) { string result=string.empty; try { result = "'" + Convert.ToDateTime(str).ToString("yyyy/MM/dd") + "'"; } catch { result = System.Data.SqlTypes.SqlDateTime.Null.ToString(); } } The string Data Returned after conversion is used for insertion. I I execute Application Visual studio by compiling it works well. but when i make virtual Directory of this application and run this through browser it doesn't work. How can i solve this issue.
-
I have needed to insert and update DateTime Field in MSSQL Database Table. I am using Text Querry. My input Control is TextBox where the format of Date is "dd/MM/yyyy". In Database it should be save as "yyyy-MM-dd". I have used a Function to convert Date Format as: public static string ConvertToDate(string pstr) { string result=string.empty; try { result = "'" + Convert.ToDateTime(str).ToString("yyyy/MM/dd") + "'"; } catch { result = System.Data.SqlTypes.SqlDateTime.Null.ToString(); } } The string Data Returned after conversion is used for insertion. I I execute Application Visual studio by compiling it works well. but when i make virtual Directory of this application and run this through browser it doesn't work. How can i solve this issue.
As far as I know there should not be any case for not working the same code from virtual directory if it works from visual studio. Are you sure you set the correct path while configuring the virtual directory?
-
I have needed to insert and update DateTime Field in MSSQL Database Table. I am using Text Querry. My input Control is TextBox where the format of Date is "dd/MM/yyyy". In Database it should be save as "yyyy-MM-dd". I have used a Function to convert Date Format as: public static string ConvertToDate(string pstr) { string result=string.empty; try { result = "'" + Convert.ToDateTime(str).ToString("yyyy/MM/dd") + "'"; } catch { result = System.Data.SqlTypes.SqlDateTime.Null.ToString(); } } The string Data Returned after conversion is used for insertion. I I execute Application Visual studio by compiling it works well. but when i make virtual Directory of this application and run this through browser it doesn't work. How can i solve this issue.
Perhaps providing us some error messages from your application can help us to figure out what the problem is.
-
Perhaps providing us some error messages from your application can help us to figure out what the problem is.
Error when using virtual Directory is: Exception Details: System.FormatException: String was not recognized as a valid DateTime. Source Error: Line 298: if (str.ToString().Trim().Length > 0) Line 299: { Line 300: result = "'" + Convert.ToDateTime(str).ToString("MM/dd/yyyy") + "'"; Line 301: } Line 302: else But it is ok from Visual Studio.
-
Error when using virtual Directory is: Exception Details: System.FormatException: String was not recognized as a valid DateTime. Source Error: Line 298: if (str.ToString().Trim().Length > 0) Line 299: { Line 300: result = "'" + Convert.ToDateTime(str).ToString("MM/dd/yyyy") + "'"; Line 301: } Line 302: else But it is ok from Visual Studio.
It will never be catched during compile time since its a runtime error. Try using DateTime.TryParse method. http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx[^]
-
I have needed to insert and update DateTime Field in MSSQL Database Table. I am using Text Querry. My input Control is TextBox where the format of Date is "dd/MM/yyyy". In Database it should be save as "yyyy-MM-dd". I have used a Function to convert Date Format as: public static string ConvertToDate(string pstr) { string result=string.empty; try { result = "'" + Convert.ToDateTime(str).ToString("yyyy/MM/dd") + "'"; } catch { result = System.Data.SqlTypes.SqlDateTime.Null.ToString(); } } The string Data Returned after conversion is used for insertion. I I execute Application Visual studio by compiling it works well. but when i make virtual Directory of this application and run this through browser it doesn't work. How can i solve this issue.
Are you sure the current culture is the same between IIS and Visual Studio...? Try outputting
System.Globalization.CultureInfo.CurrentCulture.ToString()
on both sites and see if there are any differences. If this is different, thenConvert.ToDateTime(str)
will complain.modified on Tuesday, June 8, 2010 4:16 AM
-
Are you sure the current culture is the same between IIS and Visual Studio...? Try outputting
System.Globalization.CultureInfo.CurrentCulture.ToString()
on both sites and see if there are any differences. If this is different, thenConvert.ToDateTime(str)
will complain.modified on Tuesday, June 8, 2010 4:16 AM
This problem is solved after changing Globalization setting in Web.Config File. Previously i had used en_US culture, Now it is changed to en_GB then problem is solved. Thank you all for yours remarkable helps.
-
I have needed to insert and update DateTime Field in MSSQL Database Table. I am using Text Querry. My input Control is TextBox where the format of Date is "dd/MM/yyyy". In Database it should be save as "yyyy-MM-dd". I have used a Function to convert Date Format as: public static string ConvertToDate(string pstr) { string result=string.empty; try { result = "'" + Convert.ToDateTime(str).ToString("yyyy/MM/dd") + "'"; } catch { result = System.Data.SqlTypes.SqlDateTime.Null.ToString(); } } The string Data Returned after conversion is used for insertion. I I execute Application Visual studio by compiling it works well. but when i make virtual Directory of this application and run this through browser it doesn't work. How can i solve this issue.
This might be a culture problem Try Converting using those 2 options result = Left(Convert.ToString(CDate(pstr), New CultureInfo("ja-JP")), 10) or if your date in Dd/MM/YYYY format then try using very basic method like result = Strings.Right(pstr, 4) & "-" & Strings.Right(Strings.Left(pstr, 5), 2) & "-" & Strings.Left(pstr, 2)