I import excel file and show on datagridview
I want insert all row datagridview to mysql. But i receive error: Incorrect date value:'11/14/1991 12:00:00 AM' for column 'Ngaysinh at row 1.
Mysql : i using date : yyyy/mm/dd
datagridview show : mm/dd/yyyy. i don't know error: '12:00:00AM ' ????
column 3,5 : date
This is my code:

for (int i = 0; i < dataGridView1.Rows.Count; i++) // dòng
                {
                        string str = "Server=localhost;Port=3306;Database=sinhvien;Uid=root;Pwd=1234;";
                        MySqlConnection constr = new MySqlConnection(str);
                        constr.Open(); 
                        String cmdText = "INSERT INTO danhsachsinhvien (idSinhVien, Ho, Ten, Ngaysinh, Ghichu,Ngaythi ,Buoithi) VALUES ('"
                                                   + dataGridView1.Rows[i].Cells[0].Value + "','"
                                                   + dataGridView1.Rows[i].Cells[1].Value + "','"
                                                   + dataGridView1.Rows[i].Cells[2].Value + "','"
                                                   + dataGridView1.Rows[i].Cells[3].Value + "','"
                                                   + dataGridView1.Rows[i].Cells[4].Value + "','"
                                                   + dataGridView1.Rows[i].Cells[5].Value + "','"
                                                   + dataGridView1.Rows[i].Cells[6].Value + "')";
                        MySqlCommand cmd = new MySqlCommand(cmdText, constr);
                        cmd.ExecuteNonQuery();

                }

dataGridView1.Rows[i].Cells[0].Value

Should be

Convert.ToDateTime(dataGridView1.Rows[i].Cells[0].Value).ToString("yyyy-MM-dd HH:mm:ss.fff")

Edited 1 Year Ago by J.C. SolvoTerra

You should follow the format of MySQL in order to save it from MySQL.

the format is yyyy-MM-dd HH:mm:ss ex. 2015-01-01 00:00:00
yyyy for four digit year
MM for two digit month
dd for two digit day
HH for two digit military hour time
mm for two digit minute
ss for two digit second

Please read string format for more info.

Sir. J.C. SolvoTerra is right. convert the data of datagrid to date in order to save it in MySQL.

Edited 1 Year Ago by JerrimePatient

This article has been dead for over six months. Start a new discussion instead.