1
I have an Insert that receives some parameters, more at the moment that mount the sql statement this giving conversion error:
Error Converting data type varchar to Numeric.
//http://localhost:7630/api/AspNetWebApi/cadastrar/jogo/4512/20.01/20.10/5
[HttpPost]
[Route("cadastrar/jogo/{nJogo}/{valor}/{total}/{idusuario}")]
public HttpResponseMessage Cadastro(int nJogo, decimal valor, decimal total, int idusuario)
{
try
{
var tTabela = new JogoDetalheAplicacao();
tTabela.Inseri(nJogo, valor,total,idusuario);
return Request.CreateResponse(HttpStatusCode.OK, "Cadastro realizado.");
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
public void Inseri(int nJogo, decimal valor, decimal total, int idusuario)
{
var strQuery = "";
strQuery += "INSERT INTO TB_JOGO_DETALHE_TEMP (NUMERO_JOGO, VALOR_JOGO,VALOR_TOTAL,IDUSUARIO)";
strQuery += string.Format(" VALUES (");
strQuery += string.Format(" {0}, ", nJogo);
strQuery += string.Format(CultureInfo.InvariantCulture, " {0:0.00}, ", valor);
strQuery += string.Format(CultureInfo.InvariantCulture, " {0:0.00}, ", total);
strQuery += string.Format(" {0} ", idusuario);
strQuery += string.Format(" ) ");
using (contexto = new Contexto())
{
contexto.ExecutaComando(strQuery);
}
}
Result of the final sql query:
INSERT INTO TB_JOGO_DETALHE_TEMP (NUMERO_JOGO, VALOR_JOGO,VALOR_TOTAL,IDUSUARIO) VALUES ('4512','20,01','20,10','5' )
The right thing would be to come like this:
INSERT INTO TB_JOGO_DETALHE_TEMP (NUMERO_JOGO, VALOR_JOGO,VALOR_TOTAL,IDUSUARIO) VALUES ('4512',20.01,20.10,'5' )
I tested here and the query was as expected, from a look : https://dotnetfiddle.net/0On7LU
– Joel Ramos Michaliszen
@Joelramosmichaliszen, in his example of right, more in mine I have done various types of conversion is not worked
– Harry
What are the types of the fields??: (NUMERO_JOGO, VALOR_JOGO,VALOR_TOTAL,IDUSUARIO)
– Joel Ramos Michaliszen
@Joelramosmichaliszen, int nJogo, decimal value, decimal total, int idusuario
– Harry
Yes, but type of the field in the bank, ex: Top(10,2)
– Joel Ramos Michaliszen
Numeric(8, 2) for values, the rest is int
– Harry