insert and update date values in DB using a makedtextbox C#

Asked

Viewed 41 times

0

Hello, while I was developing a system to manage the records in DB error is happening that when entering the values the data records become empty and when trying to update the information listing it gives an error that says it cannot format the values in Datetime.

if (status == "novo")
                {  
                    Conexao.cmdMySql.CommandText = "INSERT INTO eventos (id_event,nome_event,descricao_event,local_event,data_event,latitude_event,longitude_event) VALUES('" + codigo_tb.Text + "','" + nome_tb.Text + "','" + descricao_tb.Text + "','" + local_tb.Text + "','" + maskedTextBox1.Text + "','" + latitude_tb.Text + "','" + longitude_tb.Text + "')";
                    Conexao.cmdMySql.ExecuteNonQuery();
                    MessageBox.Show("Registro salvo com sucesso", "Salvar", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (status == "editar")
                {
                    DateTime data_datetime = DateTime.Now;
                    string format = "yyyy-MM-dd";
                    Conexao.cmdMySql.CommandText = "UPDATE eventos SET id_event='" + codigo_tb.Text + "',nome_event='" + nome_tb.Text + "',descricao_event='" + descricao_tb.Text + "',local_event='" + local_tb.Text + "',data_event='" + data_datetime.ToString(format) + "',latitude_event='" + latitude_tb.Text + "',longitude_event='" + longitude_tb.Text + "' WHERE id_event='" + event_lstv.Items[event_lstv.FocusedItem.Index].Text + "'";
                    Conexao.cmdMySql.ExecuteNonQuery();
                    MessageBox.Show("Registro salvo com sucesso", "Salvar", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    MessageBox.Show("Alterado com sucesso", "Alterar", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

And this is the error message that happens and the part of the code that he’s referencing: inserir a descrição da imagem aqui

Conexao.reader = Conexao.cmdMySql.ExecuteReader();
            while (Conexao.reader.Read())
            { //repete até finalizar os campos

                ListViewItem list = new ListViewItem(Conexao.reader[0].ToString()); //este comando é um objeto que liga sa informações do banco para o componente grafico list view organizando por índice.
                                                                                    //O índice 0 representa a coluna do ID que está oculta
                list.SubItems.Add(Conexao.reader[1].ToString());
                list.SubItems.Add(Conexao.reader[2].ToString());
                list.SubItems.Add(Conexao.reader[3].ToString());
                list.SubItems.Add(Conexao.reader[4].ToString());
                list.SubItems.Add(Conexao.reader[5].ToString());
                list.SubItems.Add(Conexao.reader[6].ToString());
                //Exibe os dados das colunas. Ocultando a coluna ID.

                event_lstv.Items.AddRange(new ListViewItem[] { list });
            }
            Conexao.reader.Close();

        }
  • You are taking the MYSQL return object and playing on a DATETIME, do not know if the code that gives this error is there.

  • Why do you format Tada? data_datetime.Tostring(format) , it would not be enough to use . Date ? data_datetime.Date;

  • Thanks. I think the format was the same error after using . Date it went back to normal.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.