0
Good evening guys, I’m new in C# and would like if possible a help, I created an Enum with client codes and I’m trying to pass its value through a validation and I’m getting string to int conversion error,
"Conversion failed when Converting the varchar value 'Client' to data type int."
I believe my Enum is already int and I do not know why I get the error, follow below:
My Enum:
public class Cliente
{
public Clientes ClienteId { get; set; }
}
public enum Clientes : int
{
Cliente = 1,
Cliente2 = 2
}
at Index I get him like this:
@Html.EnumDropDownListFor(model => model.Cliente,"--Escolha o cliente--")
The query :
public DataTable ConsultarPedido(string pedidoid,Clientes cliente)
{
try
{
var consultar = $@"SELECT p.pedidoid, p.clienteid, p.statuspedido FROM dbo.Pedido p JOIN dbo.ItemPedido ip ON p.PedidoId = ip.PedidoId WHERE p.PedidoId ='{pedidoid}' AND p.ClienteId='{cliente}'";
return new AcessoBanco().RunQuery(consultar).Tables[0];
}
Ruan thanks so much for the help, in my controller I did this:public Actionresult Index(Request items) atin atin var pedido = new Pedido(); Viewdata["Pedidoid"] = pedido.Pedidoid; Viewdata["Client"] = (int)pedidos.Cliente; I don’t know where to make the conversion.
– Mfecosta
Try to make the conversion during the assembly of your query (in the interpolation
{cliente}
). The interpolation I mentioned will call the methodToString()
in what is available, aiming to format thestring
correctly. The error you are receiving calls yourstring
ofvarchar
, then most likely it is coming from the API connection to your database, ie during the execution of the converted query.– Ruan Montelo
Okay got it, thank you very much Ruan I’ll adjust.
– Mfecosta