Cast error while running the Rest service

Asked

Viewed 39 times

0

I’m making that mistake:

The 'Qtde' Property on 'Itenslib' 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 it?

[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);

        }
    }

3 answers

1

I decided to change the method. I didn’t 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;
        }

And of course the call

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

1

The problem is here:

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

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

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

Make this one a Nullable<float> that will work.

[Column("QTDE")]
public float? Qtde { get; set; }
  • Yes, it is nullable(string), I passed everything to string in DTO, due to a problem that we debated here and I have not found solution, except to do this "Gambi", passing the fields float and double to string. Even you participated a lot in this situation, which gave me a lot of light on various issues.

  • My mistake, I did not see that it was string and string accepts normal null. Sorry. Are you sure it is the same error that continues? It is not a new?

  • Man, I just don’t understand if one way it works and another way it doesn’t. For me the problem is solved, but understanding is fundamental, not to occur and to be skating in a problem that is not a problem in itself, but a logical error. I will continue my reading on the subject.

  • It goes like this: When you make a query without Enumerable.Select() you bring all the bank lines, something like a SELECT * FROM ITENSLIB WHERE.... So if you don’t have a value on the line it will bring nothing and will break your property, it’s different than making a contexto.ItensLibs.Select and do not bring this property from the bank, in which case an entity with its default value will be created (which in the case of the float will be 0). In the case of DTO it is not a problem to be null because string in C# accepts null by default.

  • Well, okay, thanks for the explanation. I understood why things, in this context, but the question of the cast, when I finish this project of 15 days that is over a month, I will return to the subject.

1

In SQL the type float is equivalent to double.

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

C# Reference

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

  • Murarialex, in this case and in other posts of mine on the subject, I changed the float to Single or double and the error persisted. I even have some unsolved posts here, about CAST, I had to turn into my DTO all floats and doubles in string so that my service could work.

  • Even changing the property on C# Qtde of float for double doesn’t work? If you can, do this test and show which error message it returns.

Browser other questions tagged

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