Problem with Convert.ToDateTime in asp.net - c#

I have an application that works without any problem in a spanish server.
When i uploaded the application into the online server (an english windows), im getting exceptions (of type "input string is not a valid Datetime/Int32") with Convert.ToDateTime and Convert.ToInt32. Are any web.config line that could help me in this matter? I tried adding a globalization element with the Spanish culture, but didnt worked.
Could you give me a hand?
Thanks in advance.
Josema.

You need:
System.Globalization.CultureInfo culture =
new System.Globalization.CultureInfo("es-ES");
DateTime myDateTime = Convert.ToDateTime(string, culture);

Are you specifying a CultureInfo argument, as an IFormatProvider in your String.Format() calls?

You might have set uiculture instead of culture in the globalization element, see: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx.
...
<globalization culture="es-MX" />
...
You can also try using a more specific culture (like the one above es - mexico).
Ps. I have a site working like that (actually with culture="en" as in my case I needed to force english as my development computer was configured with spanish at the time).

Related

Why wpf ResourceManager GetString (localization) returns english only

I have a localization issue.
Here,in below code (auto generated in designer file) the str always returns english values even if the application is in other locale.
.
ResourceManager rm = new System.Resources.ResourceManager("A.b.Properties.Resources", typeof(Resources).Assembly);
.
.
var culture = Thread.CurrentThread.CurrentUICulture; //zh-CN selected as locale
var str = rm.GetString(key, culture); //always returns english :(
.
My resource file sits under the zh-CN folder wherever the executables got built:
"..\zh-CN\A.b.resources.dll" (checked the dll internally, it has the all the strings-values available in cn language)
Project name is: A.b
Namespace in the designer file is: A.b.Properties
I am not able to figure out why the application returns only the english values and not the locale specific ones!
I tried it a lot but unable to figure out whats wrong!
Though i, noticed that the _resourceSets [zh-CN] object Values has all english items in it.
Any idea/reference is much much appreciated.
At application startup, my application determines my preferred language culture, gets the system CultureInfo object for it, and sets the following two values. Are you doing something like this?
System.Threading.Thread.CurrentThread.CurrentUICulture = desiredCulture;
CultureInfo.DefaultThreadCurrentUICulture = desiredCulture;
Also, I don't know if it matters but all of my language-specific resource DLLs live in subfolders that are just the TwoLetterISOLanguageName code from the CultureInfo in question.
So for example, my French translations live in the "fr" subfolder. Are you sure your resources should not live in a "\zh" subfolder, instead of "\zh-cn"?

Convert.ToDateTime() not working on Server.. Give format exception

TxtPolCreatedDate.Text = Convert.ToDateTime(TxtPolCreatedDate.Text).ToString("dd/MMM/yyyy");
Its working fine on local machine but not working on server . I have also checked for the Date.Parse() and specify culture as suggested on
https://msdn.microsoft.com/en-us/library/9xk1h71t(v=vs.110).aspx.
But not working..
Try this:
DateTime.ParseExact(TxtPolCreatedDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Because Convert.ToDateTime uses the current culture of the server and probably the server's current culture's DateTime format is different from yours. So the solution is using ParseExact method and pass the culture explicitly.
Reference : DateTime.ParseExact Method (String, String, IFormatProvider)
Thanks a lot for your response.. But this is not working in my case.
I got solution.. If we add
[<globalization uiCulture="en" culture="en-GB" enableClientBasedCulture="true "/>]
in web.config file . then it will not give format exception..

Problem with changing language using CultureInfo

Language is not getting changed when we are giving specific culture like "fr-FR" through Resource File.please help me out of it if any one knows ,Thanks in advance.
CultureInfo cinfo = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentCulture = cinfo;
Thread.CurrentThread.CurrentUICulture = cinfo;
The below code indicate that accessing the value through resource1 file according to the culture .
_inboxpage.Text = Resource1.Ready;
The resource files need to have a special naming convention to work transparently with different culture information.
You create a separate resource file for each language that you want to support or for a language and culture. Have one separate neutral resource file for the application to fall back upon in case the required key/value pair is not found.
ex:
Resources.resx //neutral resource file
Resources.fr.resx //french specific file
Resources.fr-FR.resx //French language for France
and so on.
You can get more details here:http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
Finally i got a solution that while doing Localization in Plugin Application we need to copy that culture folders like "fr-FR" to corresponding Main Application then it will works fine.

DateTime parsed differently in different environments?

I am inserting some data into a SharePoint list (via web services) and on my local machine I set a date field like this (hard coded in this example)
<Field Name='TimeOnScene'>" + DateTime.Parse("13/12/2011 1:00").ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Field>
and it works fine on my local machine, but if I publish it to our web host and run the exact same code I get
{"Message":"String was not recognized as a valid DateTime.","StackTrace":"
//
//
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)\r\n at System.DateTime.Parse(String s)\r\n "ExceptionType":"System.FormatException"}
How is this possible?
Thanks in advance.
Edit:
we also moved from host to another two weeks ago and never had this issue before.
Use DateTime.ParseExact instead of Parse, the converting will be like the following code.
CurDate = DateTime.ParseExact(YourDateString, "dd/MM/yyyy hh:mm", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None)
The host could have different local in set on there machine. in the documentation
The string s is parsed using formatting information in the current DateTimeFormatInfo object, which is supplied implicitly by the current thread culture.

c# and Date Culture Problems

I've written a asp.net app and one of my postback routines simply saves user submitted form data to a sql 2005 db. All runs great on my development machine but when I deploy to the live site I'm getting invalid dates from my parse date checker.
Basically it is expecting an american date format on the live machine but this is not what I want. The user needs to be able to enter in dd/MM/yyyy format. So a valid date like 21/10/2009 returns errors on live server but not on my dev machine. Below is the code that throws the exception.
DateTime dt;
dt = DateTime.Parse(sdate);
//sdate in GB dd/MM/yyyy format
Is it possible to force the parse routine to expect the date in dd/MM/yyyy format?
Do like this:
System.Globalization.CultureInfo cultureinfo =
new System.Globalization.CultureInfo("en-gb");
DateTime dt = DateTime.Parse("13/12/2009", cultureinfo);
You can use DateTime.ParseExact to specify an expected format.
Another option is to specify the culture you wish to use in the web.config file:
<system.web>
...
<globalization
culture="da-DK"
uiCulture="da-DK" requestEncoding="utf-8" responseEncoding="utf-8"
/>
</system.web>
Yes, ParseExact will do that as mentioned by Matt.
The code would be something like:
dt = DateTime.ParseExact(sdate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

Categories