Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i need solution for System.FormatException: 'Input string was not in a correct format.'
i design desktop Application for Clinic
i want to get the result ( cost - paid)

i debug the app and compiler get error on

RemindTextBox.Text = Convert.ToString(Convert.ToDouble(PatientCostTextBox.Text) - Convert.ToDouble(PayedTextBox.Text));

What I have tried:

fuction to add new Patient



C#
private void btn_Add_Click(object sender, EventArgs e)
        {
            

                      if (radioButton_Male.Checked == true)
            {
                gender = "Male";
            }
               
            else
            {
                gender = "Female";
            }



                byte[] img;
                MemoryStream ms1;
                ms1 = new MemoryStream();

                pictureBox1.Image.Save(ms1, pictureBox1.Image.RawFormat);
                img = ms1.ToArray();

            RemindTextBox.Text = Convert.ToString(Convert.ToDouble(PatientCostTextBox.Text) - Convert.ToDouble(PayedTextBox.Text));

                Add_New_Patient.Add_Patient(

                 Convert.ToInt32(PatientIDTextBox.Text),
                 PatientTCTextBox.Text,
                 PatientNameTextBox.Text,
                 PatientAgeTextBox.Text,
                 gender,
                 PatientPhoneTextBox.Text,
                 comboBox_ShowState.SelectedIndex.ToString(),
                 dateTimeNOW.Value.Date.ToString(),
                 EmailTextBox.Text,
                 Convert.ToDouble(PatientCostTextBox.Text),
              Convert.ToDouble(PayedTextBox.Text),
                Convert.ToDouble(RemindTextBox.Text),
                 img,
                 NOTES_TextBox.Text
                 );
            
           
            

    


        }


basic class

C#
public static class Add_New_Patient
 {

     public static void Add_Patient( int Patient_ID,string Patient_Identity, string Patient_Name, string Age, string Gender, string Phone, string Patient_State, string Patient_Date,
         string Patient_Email, double Cost, double Paid, double Remain,  byte[] Patient_img, string NOTES


         )
     {
         DXL.DXL ob = new DXL.DXL();
         ob.open();

         SqlParameter[] p = new SqlParameter[14];
         p[0] = new SqlParameter("@Patient_ID", SqlDbType.Int);
         p[0].Value = Patient_ID;

         p[1] = new SqlParameter("@Patient_Identity", SqlDbType.NVarChar,50);
         p[1].Value = Patient_Identity;

         p[2] = new SqlParameter("@Patient_Name", SqlDbType.NVarChar, 50);
         p[2].Value = Patient_Name;

         p[3] = new SqlParameter("@Age", SqlDbType.NVarChar, 50);
         p[3].Value = Age;


         p[4] = new SqlParameter("@Gender", SqlDbType.NVarChar, 50);
         p[4].Value = Gender;


         p[5] = new SqlParameter("@Phone", SqlDbType.NVarChar, 50);
         p[5].Value = Phone;

         p[6] = new SqlParameter("@Patient_State", SqlDbType.NVarChar, 50);
         p[6].Value = Patient_State;


         p[7] = new SqlParameter("@Patient_Date", SqlDbType.Date);
         p[7].Value =Patient_Date;

         p[8] = new SqlParameter("@Patient_Email", SqlDbType.NVarChar, 50);
         p[8].Value = Patient_Email;

         p[9] = new SqlParameter("@Cost", SqlDbType.Float);
         p[9].Value = Cost;

         p[10] = new SqlParameter("@Paid", SqlDbType.Float);
         p[10].Value = Paid;

         p[11] = new SqlParameter("@Remain", SqlDbType.Float);
         p[11].Value = Remain;


         p[12] = new SqlParameter("@Patient_img", SqlDbType.Image);
         p[12].Value = Patient_img;

         p[13] = new SqlParameter("@NOTES", SqlDbType.NVarChar, 50);
         p[13].Value = NOTES;


         ob.RUA("New_Patient", p);
         ob.close();


     }
Posted
Updated 25-Feb-22 8:32am
v2

1 solution

RemindTextBox.Text = Convert.ToString(Convert.ToDouble(PatientCostTextBox.Text) - Convert.ToDouble(PayedTextBox.Text));


First off, don't use Convert methods with user input values - they make mistakes and the Convert methods always throw an exception when that happens. Instead, use the TryParse methods:
C#
double patientCost;
if (!double.TryParse(PatientCostTextBox.Text, out patientCost))
   {
   ... report problem to user ...
   return;
   }
Now patientCost will always contain a valid double value.
Do that at the top of the method, and repeat it for each input value you will process.

But why are you converting a string to a double in order to convert it back to a string?
 
Share this answer
 

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