Enum Class which I intend to extract male and female values here
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime DoB { get; set; }
public enum MF { Male,Female }
public MF Gender { get; set; } //here's my target
}
Data Access Layer here's where I extract from my DB msql
public IList<Employee> GetAllEmployees()
{
string query = "EXEC GetAllEmployees";
string constring = "Data Source=.\\SQLEXPRESS;Initial Catalog=ProjectDatabase;Integrated Security=True;Pooling=False";
IList<Employee> AllEmployees = new List<Employee> {};
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
List<Employee> customers = new List<Employee>();
//cmd.CommandType = CommandType.Text;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
AllEmployees.Add(new Employee
{
ID = Convert.ToInt32(sdr["ID"]),
Name = sdr["Name"].ToString(),
DoB = (DateTime)sdr["DoB"],
Gender = (MF)Enum.Parse(typeof(MF), (string)sdr["Gender"]), //here's where it extracts the Gender value as 0 or 1 instead in plain words
});
}
}
con.Close();
return AllEmployees;
}
}
}
Business Logic Layer self explanatory
public IList<Employee> GetEmployees(string name,MF gender)
{
EmployeeDAL EDAL = new EmployeeDAL();
if (name == "")
return EDAL.GetAllEmployees(); //Ignore this for now
else
return EDAL.GetFilteredEmployees(name,gender); //this gets used in this case
}
Controller Layer where it all starts
[Route("GetEmployees")]
[HttpPost]
public IList<Employee> GetEmployees(JArray PostData)
{
string Name = (string)PostData[0]["Name"];
MF Gender = (MF)Enum.Parse(typeof(MF), (string)PostData[0]["Gender"]);//grabed from a post request from AngularJS code
EmployeeBLL EBLL = new EmployeeBLL();
return EBLL.GetEmployees(Name,Gender);
}
Hello all, I would like to use my Gender enum to return male or female for an AngularJS POST request, but I keep getting 0 for male and one for female. How do I fix this? All additional details in the comments.
To you Enums look like strings due to the way they are named, but in truth they are numbers, at least according to a computer. You can call .ToString()
to get the name as a stirng.