Search code examples
c#datetimepicker

store datetimepicker value of c# into mysql database


Hello I want to store datetimepicker value into mysql database my code is given below

dtpDate = datetimepicker1.value.date;
dtpTime = datetimepicker2.value.Timeofday;
MySqlCommand cmd = new MySqlCommand("INSERT INTO schedule_days(schedule_name,start_time,status,days,start_date,connector_id) VALUES ('" + name + "','" + dtpTime + "','" + s + "','" + day + "','"+dtpDate+"','" + chkArray[i].Tag + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

but no value is being stored at database and at that place there is unable to read data comes. what may be the problem?


Solution

  • The Value is not being entered at MySQL database because there is mistake in your query at dtpTime and dtpDate fields.

    you shout replace it whith dtpTime.Value.TimeofDay and dtpDate.Value.Date ane new query will be like this

    dtpDate = datetimepicker1.value.date;
    dtpTime = datetimepicker2.value.Timeofday;
    MySqlCommand cmd = new MySqlCommand("INSERT INTO schedule_days(schedule_name,start_time,status,days,start_date,connector_id) VALUES ('" + name + "','" + dtpTime.Value.TimeofDay + "','" + s + "','" + day + "','"+dtpDate.Value.Date.ToString("yyyy-MM-dd HH:mm")+"','" + chkArray[i].Tag + "')", con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();