1

I need to run some validation to check that an amount is not less than 100 (for example).

The validation is in my code behind and the value will be set in my web.config but I cant figure out I check if the amount entered is less than the value set in the web.config.

The code I had before the change is

decimal amount = 0;

if (amount < 100)
{
    modelState.AddModelError("DisinvestmentsAmount", String.Format(Sippcentre.ErrorMessages.Validation.Value_NotLessThan, "100.00"));
}

Below is what I have added in my web.config

<add key="RaiseMimimum" value="100" />

This is the line of code I know i'll need to call my config file

System.Configuration.ConfigurationManager.AppSettings["RaiseMinimum"].ToString();

I'm unsure what I need to replace the following line of code with to do this check

if (amount < 100)
{

2 Answers 2

3

How about parsing your RaiseMinimum to an integer?

something like this:

string minString = System.Configuration.ConfigurationManager.AppSettings["RaiseMinimum"].ToString();
int minValue = int.Parse(minString);

if (amount < minValue)
{

Edit: When you have decimal numbers, you can also use:

decimal minValue = decimal.Parse(minString);

That's because the number "12.3" (for example) is not an integer.

2
  • I get the following error "Input string was not in a correct format.". Is this because the 'amount' is a decimal?
    – murday1983
    Sep 17, 2015 at 12:39
  • I have actually managed to resolve the issue myself. I have posted my answer
    – murday1983
    Sep 17, 2015 at 13:10
0

fixed by using the below line of code

var SellMaximum = decimal.Parse(System.Configuration.ConfigurationManager.AppSettings["SellMaximum"].ToString());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.