I want to display the data into the text boxes to update the data. but when i try to select the data to update or edit, But I got this error "String was not recognized as a valid DateTime" this is my code below.
private void dgv_Member_RowHeadMouseClick(object sender, DataGridViewCellMouseEventArgs e)
memberid = Convert.ToInt32(dgv_Member.Rows[e.RowIndex].Cells[0].Value.ToString());
dtpBDate.Text = dgv_Member.Rows[e.RowIndex].Cells[12].Value.ToString();
dtpMembership.Text = dgv_Member.Rows[e.RowIndex].Cells[22].Value.ToString();
I think that code is the main problem.
Sachin SinghPosted Feb 2, 2022, 7:08 AM
Pankajkumar PatelPosted Feb 2, 2022, 3:59 AM
Hi Mark Neil Abian,
"String was not recognized as a valid DateTime" can be resolved using ParseExact function as like:
https://stackoverflow.com/questions/2193012/string-was-not-recognized-as-a-valid-datetime-format-dd-mm-yyyy
Hope, this will help you!
Muhammad Imran AnsariPosted Feb 2, 2022, 3:54 AM
Check the format of dtpBDate and dtpMembership. It seems you have applied any custom forma on datetimepicker and the value you are getting from database is mismatched. Check the returning date format and assigned format on line
dtpBDate.Text = dgv_Member.Rows[e.RowIndex].Cells[12].Value.ToString();
dtpMembership.Text = dgv_Member.Rows[e.RowIndex].Cells[22].Value.ToString();
You can also set the format of datetimepicker control using below line
dtpBDate.Value = Convert.ToDateTime(dgv_Member.Rows[e.RowIndex].Cells[12].Value.ToString());
dtpMembership.Value = Convert.ToDateTime(dgv_Member.Rows[e.RowIndex].Cells[22].Value.ToString());