Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone. I have a real mind-bender here.

I'm currently trying to set up a new machine environment to be able to work on projects for one of our clients. The reason for us doing so is that we will eventually be handing it over to their team to develop further, and we need to provide them with instructions on how to set up and get going on their end.

Naturally, this environment I'm setting up must be able to run all the projects for this client, which we have in TFS. Setup and testing was going fine until we hit one particular project that is having an issue (FormatException) with the decimal point character we are using when converting a value to Decimal (it wants a comma instead of a dot.) We've gone through Region settings on the machine and Regedit, but still no luck. The current settings on this machine have been configured to match those of the QA server, where the project runs fine. It runs without hitch on existing machines as well. Also, I created a console app that recognises the dot as a decimal point and throws a FormatException when using a comma. In other words, it does what the client project should be doing on this new machine.


Here is the code that throws the FormatException:

C#
//note: value is the string that is used
 public class DecimalModelBinder : DefaultModelBinder
    {
        #region Implementation of IModelBinder

        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if(valueProviderResult.AttemptedValue.Equals("N.aN") ||
                valueProviderResult.AttemptedValue.Equals("NaN") ||
                valueProviderResult.AttemptedValue.Equals("Infini.ty") ||
                valueProviderResult.AttemptedValue.Equals("Infinity") ||
                string.IsNullOrEmpty(valueProviderResult.AttemptedValue))
                return 0m;

            var value = valueProviderResult.AttemptedValue.Replace(" ", "").Replace(",", "");
//The error is thrown here
            return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(value);
        }

        #endregion
    }
Posted

1 solution

We seem to have sorted out the original issue by using the following code to handle parsing the value to Decimal:

if (valueProviderResult == null)
            {
                return base.BindModel(controllerContext, bindingContext);
            }
            else
            {
                decimal tmpValue;
                if (decimal.TryParse(value, System.Globalization.NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out tmpValue))
                {
                    return tmpValue;
                }
                else
                {
                    return 0m;
                }
            }


The issue we now have is that the project doesn't seem to be picking up the DateTime settings. When running DateTime.Now in the project, it detects a date with dashes instead of slashes, which is the system's date format. I've taken a look in the IIS Globalization settings, and they are all set to Invariant Culture
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900