Click here to Skip to main content
15,885,933 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i need to Edit row in database from DataGrid View
the Problem is
i need solution for System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.'

i click f10 and compiler work
my Error in

-------|>f_Patient.dateTimeNOW.Text = this.DGV.CurrentRow.Cells[7].Value.ToString();<|------
  System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.'


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.DGV.CurrentRow.Cells[0].Value.ToString();
            f_Patient.PatientTCTextBox.Text = this.DGV.CurrentRow.Cells[1].Value.ToString();
            f_Patient.PatientNameTextBox.Text = this.DGV.CurrentRow.Cells[2].Value.ToString();
            f_Patient.PatientAgeTextBox.Text = this.DGV.CurrentRow.Cells[3].Value.ToString();
            if (this.DGV.CurrentRow.Cells[4].Value.ToString() == "Male")
            {
                f_Patient.radioButton_Male.Checked = true;
            }
            else
            {

                f_Patient.radioButton_Female.Checked = true;

            }
            f_Patient.PatientPhoneTextBox.Text = this.DGV.CurrentRow.Cells[5].Value.ToString();
            f_Patient.comboBox_ShowState.SelectedItem = (this.DGV.CurrentRow.Cells[6].Value);
            f_Patient.dateTimeNOW.Text = this.DGV.CurrentRow.Cells[7].Value.ToString();
            f_Patient.EmailTextBox.Text = this.DGV.CurrentRow.Cells[8].Value.ToString();
            f_Patient.PatientCostTextBox.Text = this.DGV.CurrentRow.Cells[9].Value.ToString();
            f_Patient.PayedTextBox.Text = this.DGV.CurrentRow.Cells[10].Value.ToString();
            f_Patient.RemindTextBox.Text = this.DGV.CurrentRow.Cells[11].Value.ToString();
            byte[] pa_img = (byte[])ShowImage.Show_Patient_Image(Convert.ToInt32(this.DGV.CurrentRow.Cells[0].Value)).Rows[0][0];
            MemoryStream ms = new MemoryStream(pa_img);
            f_Patient.pictureBox1.Image = Image.FromStream(ms);
            f_Patient.NOTES_TextBox.Text = this.DGV.CurrentRow.Cells[12].Value.ToString();
            f_Patient.ShowDialog();
        }
Posted
Updated 16-Mar-22 7:59am
v2

We can't help - we have no idea what part of that is working on what value. All we - and presumably you - know is that something is trying to parse a string to a DateTime value, and it's probably the dateTimeNOW.Text property assignment. And we have no access to that at all.

So, it's going to be up to you.
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 16-Mar-22 13:50pm    
thanks a lot for you, but I think you didn't understand my question , i already use
Visual Studio debugger and my Error Here
-------|>f_Patient.dateTimeNOW.Text = this.DGV.CurrentRow.Cells[7].Value.ToString();<|------
System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.'
OriginalGriff 16-Mar-22 14:32pm    
And?
If you are using the debugger you can look at what data iot is trying to process, and where.
We can't - we don;t have any access to your code while it is running, or the data it is processing ...
If f_Patient.dateTimeNOW is a DateTimePicker Control[^], you need to pass proper date/time value instead of string!
So, this is wrong:
f_Patient.dateTimeNOW.Text = this.DGV.CurrentRow.Cells[7].Value.ToString();

try:
f_Patient.dateTimeNOW.Value = this.DGV.CurrentRow.Cells[7].Value;


For further details, please see: Set and Return Dates with DateTimePicker Control - Windows Forms .NET Framework | Microsoft Docs[^]
 
Share this answer
 
Comments
ahmedbelal 16-Mar-22 14:06pm    
Maciej Los

I try This and My Error is

Severity Code Description Project File Line Suppression State
Error CS0266 Cannot implicitly convert type 'object' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)
Maciej Los 16-Mar-22 16:09pm    
What value is in column[7]?

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