DateTime.Parse throws an exception with Optimse Code compiler option - c#

In my project I have the line
DateTime alarmDateTime = DateTime.Parse(AlarmTimeStamp.Text);
AlarmTimeStamp.Text is for example "04/09/2015 10:23" (The computer's Region setting is English UK)
This throws a FormatException, but only when the Optimise Code compiler setting is on, i.e. a Release build. If I turn this off, it works as expected.
I'm not sure where to begin with debugging this since the debugger doesn't work correctly with optimised code.
If it's relevant, this is from a ASP.Net Web Forms application, .Net 3.5 hosted in Windows Server 2008 R2, IIS 7.5.
Edit:
I have done some testing and found that this only occurs on one of my servers. I have 2 servers configured, a development and test. This issue only occurring on my development server. There is no difference between them though. They are both built from the same image.

change your code like this..
string timeString = AlarmTimeStamp.Text;
IFormatProvider culture = new CultureInfo("en-GB", true);
DateTime alarmDateTime = DateTime.ParseExact(timeString, "dd/MM/yyyy HH:mm", culture);

Related

different Computer, different CultureInfo - Problems with datetime format "latvian"

it is a web page (Asp.Net MVC), multi-language including english, latvian, lithuvian, russian, polonia.
For selecting a date we choose the jQuery-Ui-datepicker including extra localization files for each language. (Date is given as string: "DateTime".ToString("d", new CultureInfo("lv")) )
All runs fine with all languages at my development computer (Win10, DotNet 4.5, Visual Studio 2012).
Running the web application at the server (Windows Server 2012 R2) other languages without problems. But the latvian language leads to problems.
I added trace code around the CultureInfo for Latvian 'lv'.
There is an difference between the CultureInfo.DateTimeFormat.ShortDatePattern between development notebook and web Server.
CultureInfo.DateTimeFormat.ShortDatePattern
- my computer: 'dd.MM.yyyy'
- web server : 'dd.MM.yyyy.'
the fastest solution would be: trim final point at datetime format.
(the javascript seems to work without the 'final point')
Is it a Framework Bug ?
which system needs a fix ? the server or my computer (inclusing javascript datepicker localization source)
How to solve it in a nice way (so it doesnt seems hacky)
thanks,
Mathias

String was not recognized as a valid DateTime [ASP.NET]

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.

How to resolve Date format change Issue after deployment in asp.net?

When I'm using DateTime in dd-MM-yyyy format.
When I debug my code at localhost its works fine.
But After deploying my ASP.NET web project on IIS server DateTime changes to mm-dd-yyyy format automatically.I'm facing many issues because of this problem.
I'm not able to find any solution, please let me know how can I solve this.
How can I get rid of this issue.?
1) Change the datetime format of your server from:
Control Panel -> Regional and Language Options -> Advanced
2) Open IIS and follow below steps: (For IIS7)
Click on you Website
Select .NET GLOBALIZATION option
From Culture tab, select required Culture and UI Culture.
Finally iisreset.
Your IIS probably has another Localization selected, than on your development machine.
Printing should be pretty simple if you specify the format: yourDate.ToString("dd.MM.yyyy");
Parsing a date has been a problem for me in the past. You can change the server settings or specify a CultureInfo directly in the code, like this:
DateTime.ParseExact(myDateString, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);

Entity Framework - SQL Server 2005 - IIS Server datetime issue

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.

String was not recognized as a valid Date Time error on IIS

Greeting people...i develop a web..everything working fine till deployment...my question is - why is it this error appear? because if i run the web on Visual Studio Server
everything fine...but when i deploy and run it on IIS server suddenly this error
appear..why is people? really need some help here..
string tarikh = Convert.ToDateTime(txtTarikh.Text).ToString("yyyy-MM-dd");
the line is where the cause of error..thanks in advance
For this problem you have to go to the IIS
Go to IIS -> Select your Configured Website -> Click on .NET Globalization
From .NET Globalization, select Culture and UI- Culture as English (United State) (en – US)
Restart IIS by running command as iisreset through windows command prompt
Check application is giving same problem or not
Chances are the server-side default culture is different to the one you've been using in development.
What format are you expecting the date to be in, anyway? You should either use DateTime.TryParseExact or at least use DateTime.TryParse specifying the appropriate CultureInfo. (For example, the culture of the user.)
Likewise I would suggest that you supply an appropriate CultureInfo to ToString - possibly CultureInfo.InvariantCulture.
You have different "regional setting" on your development machine and the web server.
Instead of calling Convert.ToDateTime(string), you could try to use the overloaded version Convert.ToDateTime(string, IFormatProvider) and specify in what format you expect the date to be in.
Some of us thinks that today's date is "2012-04-22" while other claims it is "4/22/2012" etc...
EDIT: Just do something like:
var ci = new CultureInfo("xx-XX");
var dateTime = Convert.ToDateTime(txtTarikh.Text, ci);
Where xx-XX is the code of the culture you want to work with. Look it up here:
http://sharpertutorials.com/list-of-culture-codes/
Because the date you get from txtTarikh.Text is not parsed as date.
Probably on your local machine, your regional settings are different from your server.
Add a log and print txtTarikh.Text to see what returns at your server and also on your local machine.
Issue is resolved by setting up .net globlization settings in IIS
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8" culture="en-US" uiCulture="en-US" />
</system.web>
change these lines in web.config and publish it again.. It worked for me.

Categories

Resources