Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting error c# system.FormatException: 'Input string was not in a correct format.' for the below code. I have tried several threads with similar heading still could not find a solution. Please help.

What I have tried:

private void txtDiscount_TextChanged(object sender, EventArgs e)
        {
            // Calculate Discount and Final Fee
            double FinalFee;
            double CourseFee1;
            double Disc;
            CourseFee1 = Convert.ToDouble(lblCourseFee.Text);
            Disc = Convert.ToDouble(txtDiscount.Text);
            FinalFee = CourseFee1 - (CourseFee1 * (Disc / 100));            

            if (Disc > 40)
            {
                string message = "Maximum Discount is 40% ";
                string title = "Discount Maximum Limit Exceeded";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Warning);
            }
            else
            {
                lblFinalFee.Text = FinalFee.ToString("N2");
            }
        }
Posted
Updated 13-Feb-22 4:04am
Comments
Richard MacCutchan 13-Feb-22 3:22am    
Which line causes the error, and what data is it trying to convert?

Never use Convert methods with user input: they throw an exception if the user mistypes.
Instead, use the double.TryParse method which returns an error:
double courseFee;
if (!double.TryParse(lblCourseFee.Text, out courseFee))
   {
   ... report problem to user ...
   return;
   }
double disc;
if (!double.TryParse(txtDiscount.Text, out disc))
   {
   ... report problem to user ...
   return;
   }
double finalFee = courseFee - courseFee * disc / 100.0;            
 
Share this answer
 
Error is on line
C#
Disc = Convert.ToDouble(txtDiscount.Text); 
OR CourseFee1 = Convert.ToDouble(lblCourseFee.Text);
because you are converting value into double. Verify you are giving the correct numeric value. There ould be a change a character or any other character coming into this filed which cause an error.
 
Share this answer
 
Thanks everyone for your valuable inputs.
I solved the issue with below code.

bool valid1 = float.TryParse(lblCourseFee.Text, out CourseFee);

bool valid2 = float.TryParse(txtDiscount.Text, out Disc);


Found it on another website. Serves the purpose.
Thanks again.
 
Share this answer
 
Comments
M Imran Ansari 13-Feb-22 13:14pm    
Great. Happy Coding!!
Richard Deeming 14-Feb-22 10:51am    
If your variables are declared as double, why are you using float.TryParse instead of double.TryParse? That will give you a compiler error:

CS1503 Argument 2: cannot convert from 'out double' to 'out float'

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