Getting this error, not all code paths return a value

Posted by Justkishor under ASP.NET MVC on 12/9/2014 | Points: 10 | Views : 2197 | Status : [Member] | Replies : 2
I want to get data from database.so I write a code in BussinessLogicLayer.cs class within that class I have get method & its raising the error "not all code paths return a value".For this my project does not build successfully.So what should i do..Plz do needful.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace BussinessLayer
{
public class EmployeeBussinessLayer
{
public IEnumerable<Employee> Employees
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
List<Employee> employees = new List<Employee>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spGetAllEmployees", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.ID = Convert.ToInt32(rdr["Id"]);
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.City = rdr["City"].ToString();
employee.DateOfBirth = Convert.ToDateTime(rdr["DateOfBirth"]);
employees.Add(employee);
}
}
}
}
}
}






Responses

Posted by: Sheonarayan on: 12/10/2014 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
0After while loop, write following

return employeees;

As the error suggest, you are not returning anything from the get so you have to return the IEnumerable<Employee> collection.

Hope this works.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Justkishor, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/11/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,
Whenever we write any getter properties,then it must be returned,in your case you need to return employees object at the end of the code as
return employees.

Justkishor, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response