I have a stored procedure on my sql database which expects a parameter and with that parameter it returns data from a view.
My stored procedure:
create procedure find_student @id varchar(6)
as
begin
select name,lastname,birthday,email,phone,city,country,university,study_year from v_scholarship
where id = @id
end
My api controller:
[HttpGet]
public IHttpActionResult MyId(string id)
{
string config = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString;
using (SqlConnection sqlcon = new SqlConnection(config))
{
SqlCommand cmd = new SqlCommand("find_student", sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", id);
sqlcon.Open();
cmd.ExecuteNonQuery();
sqlcon.Close();
return Ok();
}
}
It doesn't return any error it just returns blank. I think it is because I am not including anything at return OK() but I don't know which attribute to put in those brackets.Thank you.
EDIT:
I tried all the answers and then I found something else. Leaving this here just for the one's that may need it in the future.
[HttpGet]
public IHttpActionResult MyId(string id)
{
schoolEntity = myenitity = new schoolEntity();
var result = myenitity.find_student(id).ToList();
return Ok(result);
}
Thank you to everyone for your time, effort and patience. I really appreciate it.
return Ok();returns an empty response. The code even usesExecuteNonQuery, which means the query won't return any results. What are you trying to do? Since you used theEFtag why not actually use Entity Framework? Or a micro-ORM like Dapperr?cmd.ExecuteNonQuery();doesn't return the result of the query in the sproc, and you don't assign the returned result to any variable. This code looks far too tentative. Using plain ADO.Net isn't the best option anyway. With Dapper it's almost trivial to get data from a stored procedure. I'd explore that path.