Input string was not in a correct format là gì năm 2024

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)

    {  
        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");  
        }  
    }


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;

Error is on line

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.

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.

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

DAKTRONICS DOES NOT PROMISE THAT THE CONTENT PROVIDED HEREIN IS ERROR-FREE OR THAT ANY DEFECTS WILL BE CORRECTED, OR THAT YOUR USE OF THE CONTENT WILL PROVIDE SPECIFIC RESULTS. THE CONTENT IS DELIVERED ON AN "AS-IS" AND "AS-AVAILABLE" BASIS. ALL INFORMATION PROVIDED IN THIS ARTICLE IS SUBJECT TO CHANGE WITHOUT NOTICE. DAKTRONICS DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING ANY WARRANTIES OF ACCURACY, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. DAKTRONICS DISCLAIMS ANY AND ALL LIABILITY FOR THE ACTS, OMISSIONS AND CONDUCT OF YOU OR ANY THIRD PARTIES IN CONNECTION WITH OR RELATED TO YOUR USE OF THE CONTENT. ADJUSTMENT, REPAIR, OR SERVICE OF THE EQUIPMENT BY ANYONE OTHER THAN DAKTRONICS OR ITS AUTHORIZED REPAIR AGENTS MAY VOID THE EQUIPMENT WARRANTY. YOU ASSUME TOTAL RESPONSIBILITY FOR YOUR USE OF THE CONTENT AND ANY LINKED CONTENT. YOUR SOLE REMEDY AGAINST DAKTRONICS FOR DISSATISFACTION WITH THE CONTENT IS TO STOP USING THE CONTENT. THIS LIMITATION OF RELIEF IS A PART OF THE BARGAIN BETWEEN THE PARTIES.

The above disclaimer applies to any property damage, equipment failure, liability, infringement, or personal injury claim arising out of or in any way related to your use or application of the content, whether such claim is for breach of contract, tort, negligence or any other cause of action.