C# ASP.NET Parameter search method returning Error 500 Internal Server Error

Asked

Viewed 370 times

0

Good afternoon, I’m having a problem with a method that searches the products and returns a specific one according to the past code. I am getting error 500 Internal Server Error when I try to use url http://localhost:52444/api/product/1 to call you. However the search for the entire product list works naturally. Any idea? follows code

Code:

namespace ControleDeEstoqueAPI_.Controllers
{

    public class ProdutoConsultController : ApiController
    {

        [Route("api/Produto")]
        // GET api/ProdutoConsult
        public ObjectResult<uspConsultarProduto_Result> Get()
        {
            ControleDeEstoqueEntities entity = new ControleDeEstoqueEntities();
            var result = entity.uspConsultarProduto(null);
            return result;
        }

        [Route("api/Produto/{cod}")]
        // GET api/ProdutoConsult/5
        public List<produto> Get(int cod)
        {
            ControleDeEstoqueEntities entity = new ControleDeEstoqueEntities();
            List<produto> MyList = new List<produto>();
            var result = from produto in entity.produto where produto.pro_cod == cod select produto;
            MyList.AddRange(result);

            return MyList;
        }

    }
}

1 answer

0


Changing the body of the method was enough,

Code:

[Route("api/Produto/{cod}")]
// GET api/ProdutoConsult/5
public DataTable Get(int cod)
{
        SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=ControleDeEstoque;Integrated Security=True");
        DataTable tabela = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("Select * from Produto where pro_cod like '%" + cod + "%'", con);
        da.Fill(tabela);
        return tabela;
}

He’s returning me the result right.

Browser other questions tagged

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