How to send the Data Time Picker date to the Mysql database

Asked

Viewed 53 times

1

Speak guys I have a problem that is send my DTPentrar alias DataTimePicker, follows photo of the error and the code, if anyone can give a light thank you. Hugs

   private void btnSalvar_Click(object sender, EventArgs e)
    {
        try
        {

            conectar.Open();

            //int DataEntrada = Convert.ToInt32(DTPEntrada.Text);


            //MessageBox.Show("Conectado");

            MySqlCommand Inserir = new MySqlCommand();
            Inserir.Connection = conectar;
            Inserir.CommandText = Inserir.CommandText = "INSERT INTO Pacote (peca, Nome, Quantidade, Data_entrada) VALUES ('"
                                    + cbxPeca.Text + "', '" + txtNome.Text + "', '"
                                    + txtQuantidade.Text + "', '" + DTPEntrada + "'); ";

            Inserir.ExecuteNonQuery();
            conectar.Close();
            MessageBox.Show("Cadastro Realizado!", "Concluido",
              MessageBoxButtons.OK,
              MessageBoxIcon.Information);
            selecionarCategoria();
        }
        catch (SqlException)
        {
            MessageBox.Show("Falha na conexao!", "falha",
              MessageBoxButtons.OK,
              MessageBoxIcon.Information);
        }
    }

inserir a descrição da imagem aqui

Screenshot of the above error and the bank code below

inserir a descrição da imagem aqui

Here the error, after the suggestion of @Augusto

inserir a descrição da imagem aqui

  • Look, I’ve never worked with C# but you wouldn’t have to "treat" your date before entering ? It shouldn’t be like 2019-03-21 00:00:00 for insertion in the seat ?

  • @8bit how should I treat my date ? I’m kind of new at language

1 answer

2

Try instead of passing the concatenated parameters, go through Mysql. That is to say:

    Inserir.CommandText = "INSERT INTO Pacote (peca, Nome, Quantidade, Data_entrada) VALUES (@peca, @nome, @quantidade, @dataentrada)";

inserir.Parameters.AddWithValue("@peca",cbxPeca.Text);
inserir.Parameters.AddWithValue("@nome",txtNome.Text);
inserir.Parameters.AddWithValue("@quantidade",txtQuantidade.Text);
inserir.Parameters.AddWithValue("@dataentrada",DTPEntrada .Text);

Or even if you can’t pass the values directly from the components, create a model class, assign the values, and pass them by parameter to the method. PS.: Best way to work.

  • 1

    Ah, and remember that in case I helped you don’t forget to evaluate the answers! D

  • then I did as you suggested, but still the date information is not sent, it says the value is not correct, I will send the photo of the error in the question

  • I have also changed the question of DATETIME in the database for DATE

  • 1

    Then try Dtpentrada.Value?

  • 1

    sqlCommand.Parameters.Add("@datainput", Sqldbtype.Datetime). Value = Dtpentrada.Value; If it still doesn’t work out, try to convert the field to Datetime before passing the parameter.

  • 1

    The property that brings the date of the datapicker is value. Try this: insert.Parameters.Addwithvalue("@datainput",Dtpentrada.Value);

  • Thank you guys, it worked!

Show 2 more comments

Browser other questions tagged

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