MVC3: Submit string - parse fails on IE and Chrome - c#

I have an issue with the submitted form information. Decimal parsing fails when I try to parse a string returned through Interner Explorer or Chrome but not on Firefox or Safari. The strings looks exactly the same in Visual Studio. I made this debugging bit:
var asd3 = collection["formValue"]; // Get it from the FormCollection
var asd4 = asd3.Replace(",", "."); // Change the punctuation
var asd5 = Decimal.Parse(asd4); // Make the string into a decimal
var asd6 = Math.Round(asd5, 1); // Round it
It fails on asd5 when trying to parse the decimal out of asd4 with the error: Input string was not in a correct format.
Here's an image of the strings. Top is Firefox and below Internet Explorer.
What on earth could be the problem here?

What on earth could be the problem here?
Culture.
In your debugger inspect the value of Thread.CurrentThread.CurrentCulture and you will see differences between your browsers.
If you have a different culture set in your browser this culture will be used by ASP.NET when parsing values especially if you haven't explicitly specified the culture in your web.config:
<globalization culture="en-US" uiCulture="en-US" />
If this is set to auto then the browser culture will be used.
Another possibility is to force invariant culture when parsing to ensure that . (dot) will be the decimal separator.
var asd5 = Decimal.Parse(asd4, CultureInfo.InvariantCulture);

Related

Parsing currency string to double on webapp

I have tried string.replace("R","").... (",","") then converting to or parsing, doulbe.tryparse, convertoDouble as well as system.globalization.numberstyles, currency.
The the string R160,000 on localhost works perfect. on web app it says the string format is wrong.
on localhost the string is 160000 which works fine.
on the webapp, The string ends up as 160 000 which does not work.
Your local machine probably has the culture set to match the string you are trying to parse and the web app is running with a different default culture. You need to pass in the culture info that matches the string you are trying to parse.
For example:
var s = "R1000";
CultureInfo cInfo = new CultureInfo("af-ZA", false);
double.TryParse(s, NumberStyles.Currency, cInfo, out double result);

c# SMO Backup - how to use ExpirationDate?

I followed roughly this example to backup a database with Microsofts SMO API and the code crashed with an exception telling invalid parameter ExpirationDate. I checked the documentation which does not contain details on how to set the parameter and my intuition told me it should be in the future, right? I was curious and tested some values:
DateTime.Today.AddDays(10) -> InvalidDataException
DateTime.Today.AddDays(-10) -> works fine
DateTime.Today.AddDays(-5) -> works fine
DateTime.Today.AddDays(-4) -> works fine
DateTime.Today.AddDays(-3) -> InvalidDataException
DateTime.Today.AddDays(-1) -> InvalidDataException
DateTime.Today.AddDays(100) -> InvalidDataException
DateTime.Today.AddDays(500) -> InvalidDataException
DateTime.Today.AddDays(1000) -> works fine
Reading this 5 year-old post it could be that the internal parameter is actually not of the type DateTime? But then it would be a bug, right?
These errors are likely the result of the locale of where the Backup.ExpirationDate property is being set from. Depending on the culture this is being executed in, the DateTime.AddDays method may increment the month instead of the day as expected, leading to the inconsistent results you saw. Of the values that you tested only the negative ones should cause errors, as the range of days for a backup expiration date is 0 - 99999, with 0 indicating that the backup will never expire as stated in the documentation. Try using the CultureInfo class to define a new locale then set the expiration date. This will require a reference to the System.Globalization namespace. Running the following code gave me no errors in setting the expiration date in a backup operation using the US (en-US) culture. Just make sure that the date in the culture you convert this to matches the date you expect it to in your timezone.
using System.Globalization;
string folderPath = #"C:\YourFolder\";
Server serv = new Server(#"YourServer");
Backup bkup = new Backup();
bkup.Database = "YourDatabase";
string bkupFilePath = folderPath + bkup.Database.ToString() + ".bak";
bkup.Action = BackupActionType.Database;
bkup.Devices.AddDevice(bkupFilePath, DeviceType.File);
bkup.BackupSetName = "YourDatabase Full Backup";
bkup.BackupSetDescription = "Full backup of YourDatabase";
DateTime today = DateTime.Now;
//define current date representation with en-US culture
string newLocale = today.ToString(new CultureInfo("en-US"));
//set Backup.ExpirationDate to use new culture
bkup.ExpirationDate = Convert.ToDateTime(newLocale);
bkup.ExpirationDate.AddDays(10);
bkup.ExpirationDate.AddDays(100);
bkup.ExpirationDate.AddDays(500);
bkup.ExpirationDate.AddDays(1000);
bkup.SqlBackup(serv);
edit I am super confused. I thought this solved my issue:
My issue was that I called backup.ExpirationDate.AddDays(X) without assigning it to anything. Therefore, the software was basically using "DateTime.Now".
Solution:
backup.ExpirationDate = backup.ExpirationDate.AddDays(X);
But it didn't completely. I still get the exception if I do this:
backup.ExpirationDate = backup.ExpirationDate.AddDays(1);
No idea why this code is wrong.

IIS use wrong pt-PT

In .net 4.0, web forms and IIS 8
I have in web.config this:
<globalization culture="pt-PT" uiCulture="pt-PT" />
When I do in C# this:
ltNumber.Text = (12345.12).ToString("N");
I get this: 12 345,12
But the output must be 12.345,12
This began to look bad on windows 10. windows 7 everything was fine. What can be wrong?
The numeric ("N") format specifier uses your CurrentCulture settings.
That means your CurrentCulture has white space as a NumberGroupSeparator and , as a NumberDecimalSeparator.
As a solution, you can Clone your CurrentCulture, set these properties what ever you want, and use that culture as a second parameter in your ToString method. Like;
var clone = (CultureInfo) CultureInfo.CurrentCulture.Clone();
clone.NumberFormat.NumberDecimalSeparator = ",";
clone.NumberFormat.NumberGroupSeparator = ".";
ltNumber.Text = (12345.12).ToString("N", clone); // 12.345,12
I don't think these properties are changed based on operating system versions. It all about which culture settings you use.

ObjectDataSource fails to parse string to DateTime

I have a textbox with value that stores ValidFrom form value:
31.01.2012
and cultures set to:
<globalization culture="en-GB" uiCulture="en-GB"/>
in web.config.
And now, ObjectDataSource update method:
public static void UpdateLac(int id, DateTime ValidFrom)
{
/// ...
}
fails as I get exception that string cannot be parsed. However date in format dd.mm.yyyy (31.01.2012) is valid en-GB format and can be parsed (as far as I know). I have tested it with following code:
DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat;
var date = DateTime.Parse("31.01.2012", dtfi);
Console.Write(date.ToLongDateString());
So how come that ObjectDataSource internal conversion fails to convert string (31.01.2012) to DateTime in this example?
As far as I know the culture info is loaded directly from the OS ( in this case windows), you can check on your regional setting for the format specified. This is a screenshot from my pc:
http://imageshack.us/photo/my-images/96/engbg.png/
As you can see the format for short date is: dd/MM/aa, so maybe there is something going on with your server regional settings or the input should be: 31/01/12 instead of 31.01.2012
Hope this helps.

Problem with Convert.ToDateTime in asp.net

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).

Categories