Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I need solution for system.formatexception: 'input string wasn't in correct format

i design desktop Application for Clinic
And I design form to Edit Patient's Information
when debug app the error is input string wasn't in correct format

in Line
C#
f_Patient.comboBox_ShowState.SelectedIndex =Convert.ToInt32( this.AllPatient_dataGridView.CurrentRow.Cells[6].Value);


What I have tried:

C#
private void btn_Update_FromGrid_Click(object sender, EventArgs e)
        {
            Forms.F_Patient f_Patient = new F_Patient();
            f_Patient.btn_Add_NewPatient.Visible = false;
            f_Patient.btn_Delete.Visible = false;
            f_Patient.btn_Search.Visible = false;
            label1.Text = "Update Patient";
            f_Patient.PatientIDTextBox.Text = this.AllPatient_dataGridView.CurrentRow.Cells[0].Value.ToString();
            f_Patient.PatientTCTextBox.Text = this.AllPatient_dataGridView.CurrentRow.Cells[1].Value.ToString();
            f_Patient.PatientNameTextBox.Text = this.AllPatient_dataGridView.CurrentRow.Cells[2].Value.ToString();
            if (this.AllPatient_dataGridView.CurrentRow.Cells[4].Value.ToString()=="Male")
            {
                f_Patient.radioButton_Male.Checked = true;
            }
            else
            {
                f_Patient.radioButton_Female.Checked = true;
            }
            f_Patient.PatientPhoneTextBox.Text = this.AllPatient_dataGridView.CurrentRow.Cells[5].Value.ToString();
            f_Patient.comboBox_ShowState.SelectedIndex =Convert.ToInt32( this.AllPatient_dataGridView.CurrentRow.Cells[6].Value);
            f_Patient.dateTimeNOW.Text = this.AllPatient_dataGridView.CurrentRow.Cells[7].Value.ToString();
            f_Patient.EmailTextBox.Text = this.AllPatient_dataGridView.CurrentRow.Cells[8].Value.ToString();
            f_Patient.PatientCostTextBox.Text = this.AllPatient_dataGridView.CurrentRow.Cells[9].Value.ToString();
            f_Patient.PayedTextBox.Text = this.AllPatient_dataGridView.CurrentRow.Cells[10].Value.ToString();
            f_Patient.RemindTextBox.Text = this.AllPatient_dataGridView.CurrentRow.Cells[11].Value.ToString();
            byte[] pa_img = (byte[])ShowImage.Show_Patient_Image(Convert.ToInt32(this.AllPatient_dataGridView.CurrentRow.Cells[0].Value)).Rows[0][0];
            MemoryStream ms = new MemoryStream(pa_img);
            f_Patient.pictureBox1.Image = Image.FromStream(ms);
            f_Patient.ShowDialog();
        }
Posted
Updated 5-Mar-22 21:50pm
v2
Comments
Richard Deeming 7-Mar-22 4:32am    
REPOST
You have already posted this question:
I need solution for system.formatexception: 'input string was not in a correct format.'[^]

The answer has not changed.

Instead of using Convert() it is safer to use the Int32.TryParse Method[^]
Example:
using System;

public class Example
{
   public static void Main()
   {
      string[] values = { null, "160519", "9432.0", "16,667",
                          "   -322   ", "+4302", "(100);", "01FA" };
      foreach (var value in values)
      {
         int number;
         bool success = int.TryParse(value, out number);

         if (success)
         {
            Console.WriteLine($"Converted '{value}' to {number}.");
         }
         else
         {
            Console.WriteLine($"Attempted conversion of '{value ?? "<null>"}' failed.");
         }
      }
   }
}

// The example displays the following output:
//       Attempted conversion of '<null>' failed.
//       Converted '160519' to 160519.
//       Attempted conversion of '9432.0' failed.
//       Attempted conversion of '16,667' failed.
//       Converted '   -322   ' to -322.
//       Converted '+4302' to 4302.
//       Attempted conversion of '(100);' failed.
//       Attempted conversion of '01FA' failed.
 
Share this answer
 
There is no "solution": it's simply that the data you are presenting isn't a valid number.
It could be null, it could be text - we can't know, so we can't even begin to help you fix it.

So, it's going to be up to you. And the first thing to do is gather infomration on exactly what you are trying to convert.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
ahmedbelal 7-Mar-22 14:05pm    
debugger give me error here
f_Patient.comboBox_ShowState.SelectedIndex =Convert.ToInt32( this.AllPatient_dataGridView.CurrentRow.Cells[6].Value);
OriginalGriff 7-Mar-22 14:08pm    
And?
We can't see what is in your combo box. Or what is selected. Or What filled your DGV. Or What the current row is. Or what cell[6] might contain.

That's why *you* use the debugger - to gather information, not just find out where it crashed!

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