Error showing SQL server Sum in DAO layer

Asked

Viewed 53 times

0

I’m struggling to show the result of SUM in the DAO layer, the query brings the expected result, however, in the field where it is to show the sum, it does not show an error.

The process in Sql Server

ALTER PROCEDURE [dbo].[RelatorioDcv] @dateInicio date, @dataFim date
as
begin
select ve.Placa, sum(m.CombustivelAbastecido), count(v.VrId)
from Vr v inner join Mv m on
v.IdMv = m.Id inner join Veiculo ve on
v.IdVeiculo = ve.Id inner join Combustivel c on
ve.IdCombustivel = c.IdCombustivel
where convert(Date,v.DataEHoraServico) between @dateInicio and @dataFim
group by ve.Placa
end

the Code of the DAO:

public IList<Vr> Dcv(DateTime? dataInicio, DateTime? dataFim)
        {
            SqlCommand comando = new SqlCommand();
            comando.CommandType = CommandType.Text;
            comando.CommandText =
                @"
               EXECUTE RelatorioDcv @dateInicio=@dataInicio, @dataFim=@dataFim
                ";
            comando.Parameters.AddWithValue("@dataInicio", dataInicio);
            comando.Parameters.AddWithValue("@dataFim", dataFim);
            SqlDataReader dr = Conexao.Selecionar(comando);

            IList<Vr> lista = new List<Vr>();
            MvDAO mdao = new MvDAO();
            if (dr.HasRows)
            {
                while (dr.Read())
                {

                    Combustivel comb = new Combustivel();
                    Vr vr = new Vr();
                    vr.Veiculo = new Veiculo();
                    vr.Mv = new Mv();
                    vr.Veiculo.Placa = Convert.ToString(dr["Placa"]);
                    vr.Mv.CombustivelAbastecido = Convert.ToDecimal(dr["CombustivelAbastecido"]);

                    lista.Add(vr);
                }
            }
            else
            {
                lista = null;
            }
            dr.Close();
            return lista;
        }

The problem is precisely in this piece that theoretically was to show the sum

 vr.Mv.CombustivelAbastecido = Convert.ToDecimal(dr["CombustivelAbastecido"]);

I didn’t try to show Count().

  • Rafael, what error appears, specifically?

1 answer

2

Looking like this, a possible error in the code is that at the time of creation of its object Combustive(), it is not finding the field Combustivelabastecido. Try to run the database and check that all columns are correctly named, usually in the use of Sum() the column comes as "(No column name)". Try adding a name to the sum column in select, it would look like this:

    ALTER PROCEDURE [dbo].[RelatorioDcv] @dateInicio date, @dataFim date
    as
    begin
    select ve.Placa, sum(m.CombustivelAbastecido) as CombustivelAbastecido, 
    count(v.VrId)
    from Vr v inner join Mv m on
    v.IdMv = m.Id inner join Veiculo ve on
    v.IdVeiculo = ve.Id inner join Combustivel c on
    ve.IdCombustivel = c.IdCombustivel
    where convert(Date,v.DataEHoraServico) between @dateInicio and @dataFim
    group by ve.Placa
    end
  • I thought it could be something of the same kind... since the "sum" command does not create automatically searched column title.

  • Thank you very much, it worked out here, that’s right!!!

  • Very good, Rafael!

Browser other questions tagged

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