I am new to ASP.NET, and I am having issues with my API-controller function. I am calling a stored procedure and mapping the results to a model, then aggregating it into a list of Strings, but at the end of the code it returns an empty string. I noticed that execution skips the while block, and I'm not sure why, because when I set break points there it doesn't hit.
this is the output of the stored procedure when i run in in sqlserver
This is my code:
The UserModel
using System;
using System.ComponentModel.DataAnnotations;
namespace Fnk.Models{
public class UserModel{
[Required]
public string id {get; set;}
[Required]
public string Username{get; set;}
[Required]
public string Email {get; set;}
[Required]
public string UserRole {get; set;}
}
}
The Api-controller - receives an email
public IActionResult getusers([FromRoute] string email){
string conn = Configuration.GetConnectionString("FintrakDBConnection");
var columns = new List<string>(); // List<string> pTypes;
using (var con = new SqlConnection(conn)) {
var cmd = new SqlCommand("scb_getUserRoles", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
cmd.Parameters.Add(new SqlParameter {
ParameterName = "email",
Value = email,
});
con.Open();
using(SqlDataReader reader = cmd.ExecuteReader()){ ///code works up to this point
while(reader.Read()){ // breakpoint here shows the 4 columns...
var obj = new UserModel();
if (reader["id"] != DBNull.Value)
obj.id = reader["id"].ToString();
if (reader["UserName"] != DBNull.Value)
obj.Username = reader["UserName"].ToString();
if (reader["Email"] != DBNull.Value)
obj.Email = reader["Email"].ToString();
if (reader["RoleId"] != DBNull.Value)
obj.UserRole = reader["RoleId"].ToString();
columns.Add(obj.id);
columns.Add(obj.Username);
columns.Add(obj.UserRole);
columns.Add(obj.Email);
}
reader.Close();
}
con.Close();
}
return Ok(columns.ToArray()); //returns an empty array.. dont know why
}
At this point while(reader.Read()) the system shows that it has read the database and updated the reader, but execution skips the while loop and returns an empty array.
Please, what am I doing wrong?

scb_getUserRoleslooks like? Does it has output parameters or outputs a result set?