Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone assist me please am working on the C#.net application I have a method to do validation on the decimal values before insert it to the table which has a column of float type in my database. Here is the method below:
C#
public static bool validateFields(TextBox txtBox)
	{
		if(txtBox.Text != "")
		{
			double result = 0.0;
			if(!double.TryParse(txtBox.Text, out result))
			{
				return false;
			}
		}
		return true;
	}

My challenge now is when I capture the record of this format ’R12.20’ or updating the record on that is already on the table when hit the validation method where is checking if the value is double or not it’s give me a validation error.
am hooping that am clear on explaining this challenge challenge.

What I have tried:

When I tried the method below with “NumberStayes” and InvariantCulture or CurrentCulture and also change data type to decimal on the method, the cents on the record replaced like R12.20 when I save its save like 12.00 and I update the record again its saved like R1200.00 its keep on increasing
C#
public static bool validateFields(TextBox txtBox)
    {
        if (txtBox.Text != "")
        {
          
            decimal result = 0;
            NumberStyles styles = (NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands);
            if (!decimal.TryParse(txtBox.Text.Replace(" ", ""), styles, CultureInfo.CurrentCulture, out result))
            {
                return false;

            }


        }
        return true;
    }
Posted
Updated 27-Feb-19 2:19am
v2

At a guess - and that's all it can be from your description - your data is stored incorrectly, and you need to change your DB layout.
If existinG data in your DB is "R12.20" then it's stored in a VARCHAR or NVARCHAR column rather than a numeric (INT, FLOAT, DECIMAL) and the data in your DB is not valid as a floating point (or double) value in C#.
If you try to parse and string containing alphabetic letters as a double value you will get a "bad number" return from TryParse - and correctly. "R12.20" is not a double value, "12.20" can be.

So start by changing your DB and replacing the text based column with a numeric one, and converting the values over, fixing the errors as you go. always store values in the most appropriate datatype, don't just use strings because "it's easy".
If you don't fix the DB, this problem is going to come up over and over again.
 
Share this answer
 
Comments
Member 14114251 27-Feb-19 8:54am    
Thanks for your explanation although I think there something that I didn't clarify correctly the value that is capture on the form is not a like this R12.10 its like 12.10 the R is add on the form by the code to viewed on the form when the use want to check or to verify the information.
what I want to be assisted on is to check if is the any way to manipulate the validation method so that it can allow me to insert or update the values like 12.10 on the table not text and not to increment the value when am updating other values.
Thanks for your explanation although I think there something that I didn't clarify correctly the value that is capture on the form is not a like this R12.10 its like 12.10 the R is add on the form by the code to viewed on the form when the use want to check or to verify the information.
what I want to be assisted on is to check if is the any way to manipulate the validation method so that it can allow me to insert or update the values like 12.10 on the table not text and not to increment the value when am updating other values.
 
Share this answer
 

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



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