locked
The specified cast from a materialized 'System.Double' type to the 'System.Single' type is not valid. Can I solve this error RRS feed

  • Question

  • I have a DTO class in my project. So, when I run in run time I have this message:

    The specified cast from a materialized 'System.Double' type to the 'System.Single' type is not valid.

    This message appear when I put float or double type only. I mapped my DTO class, but the error continue. With decimal or int work fine. This is my DTO class:

    public class LiberacaoItensDTO
        {
            public LiberacaoItensDTO()
            {
                Mapper.Initialize(cfg =>
                {
                    cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                    .ForMember(d => d.DataLib, opt => opt.MapFrom(src => Convert.ToDateTime(DataLib)));
                    cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                    .ForMember(d => d.Juros, opt => opt.MapFrom(src => Juros.ToString("C2")));
                    cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                    .ForMember(d => d.Desconto, opt => opt.MapFrom(src => Desconto.ToString("C2")));
                    cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                    .ForMember(d => d.Acrescimo, opt => opt.MapFrom(src => Acrescimo.ToString("C2")));
                    cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                   .ForMember(d => d.Vencimento, opt => opt.MapFrom(src => Convert.ToDateTime(Vencimento)));
                    cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                    .ForMember(d => d.Entrada, opt => opt.MapFrom(src => Entrada.ToString("C2")));

                    cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                    .ForMember(d => d.Qtde, opt => opt.MapFrom(src => Qtde.ToString("C2")));
                    cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                    .ForMember(d => d.Unitario, opt => opt.MapFrom(src => Unitario.ToString("C2")));
                    cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                    .ForMember(d => d.Custo, opt => opt.MapFrom(src => Custo.ToString("C2")));
                    cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                    .ForMember(d => d.CustoDiario, opt => opt.MapFrom(src => CustoDiario.ToString("C2")));
                    cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                    .ForMember(d => d.UltCondicao, opt => opt.MapFrom(src => UltCondicao.ToString("C2")));
                    cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                    .ForMember(d => d.Total, opt => opt.MapFrom(src => Total.ToString("C2")));
                });
            }
            public int IdLiberacao { get; set; }
            [DefaultValue(0)]
            public int IdOrcamento { get; set; }
            [DefaultValue(0)]
            public int IdVendedor { get; set; }
            public string Vendedor { get; set; }
            public int IdFilial { get; set; }
            public string Filial { get; set; }
            [DefaultValue(0)]
            public float DataLib { get; set; }
            public int IdCliente { get; set; }
            public string Cliente { get; set; }
            public string TipoVenda { get; set; }
            [DefaultValue(0)]
            public float Juros { get; set; }
            [DefaultValue(0)]
            public float Desconto { get; set; }
            [DefaultValue(0)]
            public float Vencimento { get; set; }
            [DefaultValue(0)]
            public float Acrescimo { get; set; }
            [DefaultValue(0)]
            public float Entrada { get; set; }
            public string Mensagem { get; set; }
            public int IdProduto { get; set; }
            public string Produto { get; set; }
            [DefaultValue(0)]
            public float Qtde { get; set; }
            [DefaultValue(0)]
            public float Unitario { get; set; }
            [DefaultValue(0)]
            public float Custo { get; set; }
            [DefaultValue(0)]
            public float CustoDiario { get; set; }
            [DefaultValue(0)]
            public float UltCondicao { get; set; }
            [DefaultValue(0)]
            public float Total { get; set; }
        }

    below my method. This method return a DTO object type:

    public List<LiberacaoItensDTO> getAutoriza(int idorcamento)
            {

                var lista = contexto.Liberacoes
                            //.Where(lib => lib.IdOrcamento == idorcamento)
                            //.Join(contexto.ItensLibs, lib => lib.IdOrcamento, itens => itens.IdOrcamento, (lib,itens) => new { lib, itens})
                            .Where(a => a.IdOrcamento == idorcamento)
                            .Select(libera => new LiberacaoItensDTO
                            {
                                TipoVenda = libera.TipoVenda,
                                IdOrcamento = libera.IdOrcamento,
                                Juros = libera.Juros != 0.0f ? libera.Juros : 0.0f,
                                //Entrada = libera.Entrada != 0.0f ? libera.Entrada : 0.0f,
                                //Acrescimo = libera.Acrescimo != 0.0f ? libera.Acrescimo : 0.0f,
                                //Desconto = libera.Desconto != 0.0f ? libera.Desconto : 0.0f,
                                Mensagem = libera.Mensagem,
                                //DataLib = libera.DataLib != 0.0f ? libera.DataLib : 0.0f,
                                Vendedor = libera.Vendedor,
                                Cliente = libera.Cliente,
                                Filial = libera.Filial
                            }).ToList();
                return lista;
            }

    And my service

     public class LiberacaoController : ApiController
        {
            AutorizadorContext contexto = new AutorizadorContext();
            PedidoLiberacao liberacao = new PedidoLiberacao();

            [AcceptVerbs("Get")]
            public IEnumerable<LiberacaoItensDTO> getLiberacao()
            {
                return liberacao.getAutoriza(1000012093).AsEnumerable().ToList();
            }
        }

    • Moved by CoolDadTx Saturday, September 2, 2017 4:14 AM Third party related
    Friday, September 1, 2017 3:42 PM

All replies

  • The issue you're having is a runtime issue with what looks to be Automapper. These forums are for questions related to MS products only. It appears that automapper doesn't like truncating data from a double to a float. You need to post in their forums on how to solve this issue.

    Without knowing anything about automapper I notice you're trying to convert a DateTime to a double, which isn't going to work. In regular C# you can downcast using a simple typecast. Seems like you could do the same in your mapping call. But the Automapper forums should be better able to direct you in how to do it the automapper way.

    Saturday, September 2, 2017 4:14 AM