Error making select with start and end date

Asked

Viewed 53 times

0

I need to search the database for a list where it is necessary to filter by the employee, the initial date and the final date. However, when you pass the parameters and enter while, you jump to catch

public List<Nota> BuscarNotas(int funcionarioid, DateTime dataemissaoi, DateTime dataemissaof)
    {
        List<Nota> oNotas = new List<Nota>();
        using (MySqlConnection conexao = ConexaoBD.getInstancia().getConexao())
        {
            try
            {
                conexao.Open();
                MySqlCommand comando = new MySqlCommand();
                comando = conexao.CreateCommand();
                comando.CommandText = @"select * from nota where DataEmissao
                                        between DataEmissaoI = @dataemissaoi
                                        and DataEmissaoF = @dataemissaof 
                                        and FuncionarioID  = @FuncionarioID;";
                comando.Parameters.AddWithValue("FuncionarioID", funcionarioid).ToString();
                comando.Parameters.AddWithValue("DataEmissaoI", dataemissaoi).ToString();
                comando.Parameters.AddWithValue("DataEmissaoF", dataemissaof).ToString();

                MySqlDataReader reader = comando.ExecuteReader();
                while (reader.Read())
                {
                    Nota oNota = new Nota();
                    oNota.NotaID = Convert.ToInt32(reader["NotaID"].ToString());
                    oNota.FuncionarioID = Convert.ToInt32(reader["ClienteID"].ToString());
                    oNota.NumeroNota = reader["NumeroNota"].ToString();
                    oNota.DataEmissao = Convert.ToDateTime(reader["DataEmissao"].ToString());
                    oNota.Status = (Status)Convert.ToInt16(reader["Status"].ToString());
                    if ((reader["DataPagamentoFinal"] != DBNull.Value))
                        oNota.DataPagamentoFinal = Convert.ToDateTime(reader["DataPagamentoFinal"].ToString());
                    if ((reader["ValorPago"] != DBNull.Value))
                        oNota.ValorPago = Convert.ToDecimal(reader["ValorPago"].ToString());
                    if ((reader["Desconto"] != DBNull.Value))
                        oNota.Desconto = Convert.ToDecimal(reader["Desconto"].ToString());

                    oNotas.Add(oNota);
                }
            }
            catch (MySqlException ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                conexao.Close();
            }
        }
        return oNotas;
    }
}

Can someone help me?

  • in Exception has the error information, which is essential to know what is happening... it makes no sense to capture an Exception and release it again in a new Exception... and the sql syntax is wrong, in a note you have two emission dates ? initial and final ? the syntax should be where DataEmissao&#xA; between @dataemissaoi and @dataemissaof

  • you should have marked the question as solved, and opened another question to a new problem, and not edit it... please do the [Tour] to understand how the community works. Obs: those .ToString(); in Parameters.AddWithValue don’t make any sense...

1 answer

0

I’m assuming that the datetimepicker is in a windows form application.

You can use: data_Final.Value to obtain a Type object DateTime.

Or if you want the date in string format you can use the example below:

string data = data_Final.Value.ToString("dd-MM-yyyy");
  • Lodi, Thank you. The error has been fixed with data_Final.Value. However, now I’m having trouble searching the database, could you help me? I’ll edit the question

Browser other questions tagged

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