Error cast while running rest service

0

I'm having this error:

  

The 'Qtde' property on 'ItemsLib' could not be set to a   'System.Double' value. You must set this property to a non-null value   of type 'System.Single'.

What can cause this?

[Table("ITENSLIB")]
    public class ItensLib
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("ID_ITENSLIB")]
        public int IdItensLib { get; set; }
        [Column("ID_ORCAMENTO")]
        public int IdOrcamento { get; set; }
        [Column("ID_PRODUTO")]
        public int IdProduto { get; set; }
        [Column("PRODUTO")]
        public string Produto { get; set; }
        [Column("QTDE")]
        public float Qtde { get; set; }
        [Column("UNITARIO")]
        public float Unitario { get; set; }
        [Column("CUSTO")]
        public float Custo { get; set; }
        [Column("MINIMO")]
        public float Minimo { get; set; }
        [Column("TOTAL")]
        public float Total { get; set; }
        [Column("CUSTODIARIO")]
        public float CustoDiario { get; set; }
        [Column("FABRICANTE")]
        public string Fabricante { get; set; }
        [Column("ULT_CONDICAO")]
        public float UltCondicao { get; set; }
        [Column("PROGRAMA")]
        public string Programa { get; set; }
        [Column("NOME_PC")]
        public string NomePc { get; set; }
        [Column("NOME_PROCEDURE")]
        public string NomeProcedure { get; set; }
        [Column("Flag_Vencido")]
        public byte FlagVencido { get; set; }
        [Column("TipoVenda")]
        public string TipoVenda { get; set; }
        [Column("VENDA_VISTA")]
        public float VendaVista { get; set; }
        [Column("MARGEM_AVISTA")]
        public float MargemAvista { get; set; }
        [Column("QTDE_NEG_AVISTA")]
        public float QtdNegAvista { get; set; }
        [Column("VENDA_PRAZO")]
        public float VendaPrazo { get; set; }
        [Column("PM_PRAZO")]
        public float PmPrazo { get; set; }
        [Column("MARGEM_PRAZO")]
        public float MargemPrazo { get; set; }
        [Column("JUROS_PRAZO")]
        public float JurosPrazo { get; set; }
        [Column("QTDE_PRAZO")]
        public float QtdePrazo { get; set; }
        [Column("VENDA_VISTA_ANT")]
        public float VendaVistaAnt { get; set; }
        [Column("MARGEM_AVISTA_ANT")]
        public float MargemAvistaAnt { get; set; }
        [Column("QTDE_NEG_AVISTA_ANT")]
        public float QtdeNegAvistaAnt { get; set; }
        [Column("VENDA_PRAZO_ANT")]
        public float VendaPrazoAnt { get; set; }
        [Column("PM_PRAZO_ANT")]
        public float PmPrazoAnt { get; set; }
        [Column("MARGEM_PRAZO_ANT")]
        public float MargemPrazoAnt { get; set; }
        [Column("JUROS_PRAZO_ANT")]
        public float JurosPrazoAnt { get; set; }
        [Column("QTDE_PRAZO_ANT")]
        public float QtdePrazoAnt { get; set; }
        [Column("VENDA_VISTA_ANT1")]
        public float VendaVistaAnt1 { get; set; }
        [Column("MARGEM_AVISTA_ANT1")]
        public float MargemAvistaAnt1 { get; set; }
        [Column("QTDE_NEG_AVISTA_ANT1")]
        public float QtdeNegAvistaAnt1 { get; set; }
        [Column("VENDA_PRAZO_ANT1")]
        public float VendaPrazoAnt1 { get; set; }
        [Column("PM_PRAZO_ANT1")]
        public float PmPrazoAnt1 { get; set; }
        [Column("MARGEM_PRAZO_ANT1")]
        public float MargemPrazoAnt1 { get; set; }
        [Column("JUROS_PRAZO_ANT1")]
        public float JurosPrazoAnt1 { get; set; }
        [Column("QTDE_PRAZO_ANT1")]
        public float QtdePrazoAnt1 { get; set; }
    }

This is my call

public class ItensLiberacao
    {
        AutorizadorContext contexto = new AutorizadorContext();
        ItensLibDTO libDTO = new ItensLibDTO();

        [Route("itens/{id}")]
        public ItensLibDTO getItensLib(int id)
        {
            var lista = contexto.ItensLibs
                .Where(itens => itens.IdOrcamento == id)
                .Select(item => new ItensLibDTO
                {
                    Produto = item.Produto,
                    Qtde = item.Qtde.ToString(),
                    Unitario = item.Unitario.ToString(),
                    Custo = item.Custo.ToString(),
                    CustoDiario = item.CustoDiario.ToString(),
                    UltCondicao = item.UltCondicao.ToString(),
                    Total = item.Total.ToString()
                }).FirstOrDefault();

            return lista;
        }
    }

My service

[RoutePrefix("api/itens")]
    public class ItensController : ApiController
    {
        AutorizadorContext contexto = new AutorizadorContext();
        ItensLiberacao itens = new ItensLiberacao();

        [AcceptVerbs("Get")]
        public HttpResponseMessage getItensLiberacao(int id)
        {
            var _itens = contexto.ItensLibs.Where(it => it.IdOrcamento == id).FirstOrDefault();
            return Request.CreateResponse(HttpStatusCode.OK, _itens);

        }
    }
    
asked by anonymous 08.09.2017 / 19:19

3 answers

1

I resolved by changing the method. I did not understand, but it worked:

[Route("itens/{id}")]
        public List<ItensLibDTO> getItensLib(int id)
        {
            var lista = contexto.ItensLibs
                .Where(itens => itens.IdOrcamento == id)
                .Select(item => new ItensLibDTO
                {
                    Produto = item.Produto,
                    Qtde = item.Qtde.ToString(),
                    Unitario = item.Unitario.ToString(),
                    Custo = item.Custo.ToString(),
                    CustoDiario = item.CustoDiario.ToString(),
                    UltCondicao = item.UltCondicao.ToString(),
                    Total = item.Total.ToString()
                }).ToList();

            return lista;
        }

Of course the call

[AcceptVerbs("Get")]
        public IEnumerable<ItensLibDTO> getItensLiberacao(int id)
        {
            return itens.getItensLib(id).AsEnumerable().ToList();
        }
    
08.09.2017 / 19:34
1

The problem is here:

var _itens = contexto.ItensLibs.Where(it => it.IdOrcamento == id).FirstOrDefault();

When translating SQL to C #, you are assigning a null value to the QTDE property that does not accept this value.

[Column("QTDE")]
public float Qtde { get; set; }

Make this a Nullable<float> that will work.

[Column("QTDE")]
public float? Qtde { get; set; }
    
08.09.2017 / 19:35
1

In SQL the type float is equivalent to double .

In .NET, the nickname (alias) float is equivalent to single .

C # Reference

So, apparently in your database, the Qtde column is float but in C # it should be double instead of float because they are nicknames for different types.

    
08.09.2017 / 19:46