Iam trying to get a DateTime out of an textBox, where it allready is in Format of MySql DateTime. The column in the DB is also DateTime format.
However, when i press my button to save the Dates in the DB, the whole row is gonna get emptyed.
I tried around with different formats und DataTypes in DB without anny effect
private void button4_Click(object sender, EventArgs e) { MySqlConnection conn = DBUtils.GetDBConnection(); conn.Open(); string startzeit = textBoxstartzeit.Text.ToString(); DateTime start = DateTime.Parse(startzeit); string stopzeit = textBoxstopzeit.Text.ToString(); DateTime stop = DateTime.Parse(stopzeit); string pstartzeit = textBoxstopzeit.Text.ToString(); DateTime pstart = DateTime.Parse(pstartzeit); string pstopzeit = textBoxstopzeit.Text.ToString(); DateTime pstop = DateTime.Parse(pstopzeit); MySqlCommand cmdnew = conn.CreateCommand(); cmdnew.CommandType = CommandType.Text; cmdnew.CommandText = "UPDATE arbeitszeiten SET astart = '" + start + "', astop = '" + stop + "', pstart = '" + pstart + "', pstop = '" + pstop + "' WHERE id = '" + dataGridView.CurrentCell.Value + "'"; cmdnew.ExecuteNonQuery(); conn.Close(); } private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { try { MySqlConnection conn = DBUtils.GetDBConnection(); conn.Open(); MySqlCommand feedstartzeit = conn.CreateCommand(); feedstartzeit.CommandText = "SELECT astart FROM arbeitszeiten WHERE id = '" + dataGridView.CurrentCell.Value + "'"; DateTime start = Convert.ToDateTime(feedstartzeit.ExecuteScalar()); textBoxstartzeit.Text = start.ToString("yyyy-MM-dd HH:mm:ss"); MySqlCommand feedstopzeit = conn.CreateCommand(); feedstopzeit.CommandText = "SELECT astop FROM arbeitszeiten WHERE id = '" + dataGridView.CurrentCell.Value + "'"; DateTime stop = Convert.ToDateTime(feedstopzeit.ExecuteScalar()); textBoxstopzeit.Text = stop.ToString("yyyy-MM-dd HH:mm:ss"); MySqlCommand feedstartpause = conn.CreateCommand(); feedstartpause.CommandText = "SELECT pstart FROM arbeitszeiten WHERE id = '" + dataGridView.CurrentCell.Value + "'"; DateTime startpause = Convert.ToDateTime(feedstartpause.ExecuteScalar()); textBoxstartpause.Text = startpause.ToString("yyyy-MM-dd HH:mm:ss"); MySqlCommand feedstoppause = conn.CreateCommand(); feedstoppause.CommandText = "SELECT pstop FROM arbeitszeiten WHERE id = '" + dataGridView.CurrentCell.Value + "'"; DateTime stoppause = Convert.ToDateTime(feedstoppause.ExecuteScalar()); textBoxstoppause.Text = stoppause.ToString("yyyy-MM-dd HH:mm:ss"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Bitte ID auswählen", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
Button4 is the upload new data and the dataGridView part is filling the textBoxes with a preformated datetime that later get uploaded by button4
Advertisement
Answer
Allright, the by Jon Skeet suggested parametered sql request solved the problem.