Ramisha Farrukh Ramisha Farrukh - 9 months ago 28
C# Question

Not all paths return a value error c#

I am getting error Not all paths return a value. Its a syntax error how to correct it. here is my code. I am writing this code in class.

public class Employees
{
public String emp_id { get; set; }
public String emp_name { get; set; }
public String u_name { get; set; }
public String pass { get; set; }
public String mail { get; set; }
public String address { get; set; }
public String city { get; set; }
public String dob { get; set; }
public String cnic { get; set; }
public String designation { get; set; }
public String ph_no { get; set; }
}
public class @object
{
public static List<Employees> GetAllEmployees()
{
List<Employee> listemp = new List<Employee>();
string cs = ConfigurationManager.ConnectionStrings[@"Data Source = localhost; Initial Catalog=fms; User=root; Pooling=false; Integrated Security = false"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(cs))
{
MySqlCommand cmd = new MySqlCommand("Select * from emp", con);
con.Open();
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employees em = new Employees();
em.emp_id = dr[0].ToString();
em.emp_name = dr[1].ToString();
em.u_name = dr[2].ToString();
em.pass = dr[3].ToString();
em.mail = dr[4].ToString();
em.address = dr[5].ToString();
em.city = dr[6].ToString();
em.dob = dr[7].ToString();
em.cnic = dr[8].ToString();
em.designation = dr[9].ToString();
em.ph_no = dr[10].ToString();
}
listemp.Add(em);
}
}

}


I attached a pic where I am getting this error.The error is highlighted

Answer Source

You should return the List listemp . Also consider moving listemp.Add(em) inside while loop, otherwise you wont get a list

  public static List<Employees> GetAllEmployees()
            {
                List<Employee> listemp = new List<Employee>();
                string cs = ConfigurationManager.ConnectionStrings[@"Data Source = localhost; Initial Catalog=fms; User=root; Pooling=false; Integrated Security = false"].ConnectionString;
                using (MySqlConnection con = new MySqlConnection(cs))
                {
                    MySqlCommand cmd = new MySqlCommand("Select * from emp", con);
                    con.Open();
                    MySqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        Employees em = new Employees();
                        em.emp_id = dr[0].ToString();
                        em.emp_name = dr[1].ToString();
                        em.u_name = dr[2].ToString();
                        em.pass = dr[3].ToString();
                        em.mail = dr[4].ToString();
                        em.address = dr[5].ToString();
                        em.city = dr[6].ToString();
                        em.dob = dr[7].ToString();
                        em.cnic = dr[8].ToString();
                        em.designation = dr[9].ToString();
                        em.ph_no = dr[10].ToString();
                        listemp.Add(em);
                    }

                }
               return listemp;
            }