my date format in sql server is yyyy/MM/dd
and when I check the table data on c# the date format is MM/dd/yyyy
and when i run the code, i always encounter this error.
but when i set the value for BirthDate to NULL, the program runs smoothly.
help please.
Related
I developed an application in ASP.NET with MS SQL on a US based server.
Once the app was deployed to a server somewhere in Europe the dates are displayed in Dutch format.
US application displays: March 6, 2018
European application displays: maart 6, 2018
The US Sql Server is 2012 and the European SQL Server is 2014.
I've compared the SQL Server settings and there is only one difference I can find...
US SQL Server Collation = SQL_Latin1_General_CP1251_CI_AS
Europe SQL Server Collation = SQL_Latin1_General_CP1_CI_AS
I don't believe collation is the issue because I changed my US based server to match the European collation and there was no change in date format.
In C# I display the dates like this...
Convert.ToDateTime(mySQLTable.Rows[0]["datecompleted"]).ToString("MMMM d, yyyy");
The data in field "datecompleted" (DateTime) is identical on both servers.
I compared the HTML source code of both applications (View Source) and they are identical.
I can't see any difference in IIS either.
Any ideas where I should look to determine why one server is displaying dates in Dutch and the other in English?
Thanks!
Thanks to Mark Benningfield this has been resolved...
I needed to add InvariantCulture to my C#
using System.Globalization;
...
Convert.ToDateTime(mySQLTable.Rows[0]["datecompleted"]).ToString("MMMM d, yyyy", CultureInfo.InvariantCulture);
Now my date displays properly!
I have a database that is filled with log data by an external program.
One column is a timestamp of type DateTime.
Depending on the external programs host settings of time format, it either writes into the database using 24h or 12h format with AM/PM.
This query I used to get the time (on the same machine, but two database files from different external devices):
SELECT Time FROM tabData
The output (from a machine with AM/PM time setting)
2017-05-31 8:52:26 AM
and another (from a machine with 24h time setting)
2017-05-31 08:52:26
Why does the database datetime keep that 12h mode with AM/PM? Can I somehow change the format of the database datetime by SQL?
I further want to read these data into a DataTable in C#, and I'd prefer a SQL statement to read any given datetime as 24 hour datetime.
If SQLite is anything like MSSQL and Oracle, it doesn't have a Format for datetime except for its internal storage format that you shouldn't need to know anything about. The format you see only shows up when you use a Client to query that date, and then the actual format depends on the client you use, for instance general time settings chosen on the client machine.
I get the value from the user in a specific format, then I should convert it to DateTime object.
This is my code
DateTime dateGreg1 = DateTime.ParseExact(startDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
this code is working fine on my computer, but when I copied the project to another computer, this error shows up:
String was not recognized as a valid DateTime.
My question is why the program is running in the original computer, but when I copied it, it did not run?
Is there any configuration that I should do?
I am using Visual Studio 2012
The 'other computer' probably uses a different date format by default, ie US instead of UK.
In a web application, I am passing datetime without formatting it in a XML response, just item.Date (DateTime datatype) is put in code.
No formatting is done. When I run the local server it returns the date in MM/DD/YYYY format and in live environment DD/MM/YYYY. Why this change is happening?
I checked on database collation and OS settings. Live environment had English (Singapore) in regional settings. However after changing local servers to English (Singapore) still live environment DateTime format is not being produced locally.
Collation:
In Live Environment - Latin1_General_CI_AI
In Local Servers - SQL_Latin1_General_CP1_CI_AS
What could be the reason? how to resolve this rather than doing formatting in XML?
Update
As suspected, Issue is with collation, regenerated the issue locally after creating DB with collation in live environment.
Windows stores Date/Time settings per user. If IIS is running as LocalSystem, then you'll need to change the settings of the Default user:
In regedit, go here and set the appropriate values up for the locale and format that you want.
HKEY_USERS\.DEFAULT\Control Panel\International
That said, handling dates and times types under the assumption of any given format is A Really Bad Idea.
If you need date/time in a specific format for display, interop or whatever you should format the date time appropriately with one of the available string formats.
I'm working with MVC3 and Entity Framework. In my application I need to call a stored procedure in SQL Server 2005 via EF to search for some data according to datetime parameters passed.
Everything seems to be working fine in local environment. But after hosting it into IIS I am getting an exception while trying to search from date 13-08-2012 (13 is taking as month in SQL I guess)
Error says
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and
12/31/9999 11:59:59 PM
I understood error is because of the difference between date time formats between System.Datetime and SqlDatetime.
But I didn't understand why it is working without any issues in my local environment which uses same SQL Server but getting this error after hosting in IIS server.
Is there any workaround for this issue?
My issue is resolved now. Issue was with the culture settings in IIS.
I've added these line to my applications web.config and it is working fine now.
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>
For more information check out this Issue with culture settings in IIS
IT depends on the culture of the server, you can format your culture with invariant format, on your date 13-08-2012, it consider 13 as month.
//Here an example of formatting with invariant culture
CultureInfo yourCulture = CultureInfo.InvariantCulture;
yourValue.ToString("yourFormat",yourCulture));
Verify that the date time formatting. Also, DateTime doesn't have the same range as SqlDateTime. You'll run into trouble if you receive an empty value.