The code below is an exact copy of code that's working perfectly. The difference is that this code is being placed in a WCF Service Application Project whereas the working code is from a Windows Forms Application Project. The code in the foreach is unreachable which is strange because I've tested the code before and it works, returning the correct values
public IEnumerable<Employee> GetStudentDetails(string username,string password)
{
var emp = agrDb.LoginAuthentication(username, password);//procedure in the database thats returning two values
//Namely: EmployeeFirstName and EmployeeLastName
List<Employee> trainerList = new List<Employee>();
foreach (var item in emp)
{
//unreachable code here
Employee employ = new Employee();
employ.EmployeeFirstName = item.EmployeeFirstName;
employ.EmployeeLastName = item.EmployeeLastName;
trainerList.Add(employ);
//trainerList.Add(item.EmployeeLastName);
}
return trainerList;
}
The code is reachable but if it is not executed this means that
emp
is empty. You have to verify if the value ofusername
andpassword
you are using exist and thatagrDb.LoginAuthentication(username, password)
is returning a value.The code within foreach loops can be uncreachable if the array or collection is not initialised until runtime.
The above code will bring back the warning because "emp" is not initialised at design time.
I've received the same warning in my code, at various stages including within constructors. However, the runtime flow is not affected because by then, "emp" is initialised.
This may be the case with your code. Check to see where and when during the flow of the program "emp" is initialised. You may need to "step into" the program to acheive this, if it is not obvious by sight.
Maybe you are always returning a new empty array in
agrDB.LoginAuthentication()
and your IDE knows that theforeach loop
will never iterate over any items.