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);
}
}
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.
– pnet
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?
– Gabriel Coletta
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.
– pnet
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 acontexto.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.– Gabriel Coletta
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.
– pnet