简体   繁体   中英

Not all path return value - Windows Form App - C#

I am trying to create my first Employee Manager using C# in WindowsForms App. I am almost done, but there shows one error that I couldn't correct.

It's:

"CS0161 - not all code paths return a value"

I tried everything that I find here, but there was no result... I am asking for your help!

namespace WPC
{
public static class WPCDB
{

    public static SqlConnection GetConnection()
    {
        string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c: \users\volko\onedrive\документи\visual studio 2015\Projects\WPC\WPC\WPC.mdf;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connStr);
        return conn;
    }
    public static void AddEmployee(string IDnumber, string firstName, string fatherName, string lastName, string departament, float salary, DateTime date)
    {
        string insStmt = "INSERT INTO TABLE EMPLOYEE (Idnumber, firstName, fatherName, lastName, department, salary, date) VALUES (@IDnumber, @firstName, @fatherName, @lastName, @department @salary, @date)";
        SqlConnection conn = GetConnection();
        SqlCommand insCmd = new SqlCommand(insStmt, conn);
        insCmd.Parameters.AddWithValue("@IDnumber", IDnumber);
        insCmd.Parameters.AddWithValue("@firstName", firstName);
        insCmd.Parameters.AddWithValue("@fatherName", fatherName);
        insCmd.Parameters.AddWithValue("@lastName", lastName);
        insCmd.Parameters.AddWithValue("@department", departament);
        insCmd.Parameters.AddWithValue("@department", salary);
        insCmd.Parameters.AddWithValue("@department", date);
        try
        {
            conn.Open(); insCmd.ExecuteNonQuery();
        }
        catch (SqlException ex) { throw ex; } 
        finally { conn.Close(); }
    }

    public static List<Employee> GetEmployee()

    {
        List<Employee> employeelist = new List<Employee>();
        SqlConnection conn = GetConnection();
        string selStmt = "SELECT * FROM EMPLOYEE ORDER BY IDnumber, FirstName";            //employee table = table
        SqlCommand selcmd = new SqlCommand(selStmt, conn);
        try
        {
            conn.Open();
            SqlDataReader reader = selcmd.ExecuteReader();
            while (reader.Read())
            {
                Employee employee = new Employee();
                employee.EmployeeNumber = (int)reader["EmployeeNumber"];
                employee.IDnumber = reader["IDnumber"].ToString();
                employee.FirstName = reader["FirstName"].ToString();
                employee.FatherName = reader["FatherName"].ToString();
                employee.LastName = reader["LastName"].ToString();
                employee.Depart = reader["Depart"].ToString();
                employee.Salary = (float)reader["Salary"];
                employee.Date = (DateTime)reader["Date"];

                employeelist.Add(employee);

            }
            reader.Close();

        }
        catch (SqlException ex) { throw ex; }
        finally { conn.Close(); }

    }            
}
}

Thank you in advance for your time!

Your method declared with return type List<Employee> :

public static List<Employee> GetEmployee()

In your code you don't return anything from this method - you get this error.

You should either return something or declare method with void return type:

public static void GetEmployee()

I suppose you should have return statement at the end of try block (after reader.Close(); ):

return employeelist;

Your method have a return type List<>

public static List<Employee> GetEmployee()

Add this line in the List<> Block at the end

return employeelist;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM