I have a scenario where i have to return different type of objects according to a condition.
For that i have used dynamic
return type in c# 4.0.
But i couldn't achieve that.
public dynamic ValidateUser(string UserName, string Password)
{
string Result = string.Empty;
Employees clsEmployee = new Employees();
Customer clsCustomer = new Customer();
sqlConnection = new SqlConnection(Connection());
command = new SqlCommand("dbo.usp_ValidateUser", sqlConnection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@Username", SqlDbType.VarChar).Value = UserName;
command.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;
sqlConnection.Open();
SqlParameter newSqlParam = new SqlParameter();
newSqlParam.ParameterName = "@Result";
newSqlParam.SqlDbType = SqlDbType.NVarChar;
newSqlParam.Direction = ParameterDirection.Output;
newSqlParam.Size = 50;
command.Parameters.Add(newSqlParam);
SqlDataReader dr = command.ExecuteReader();
Result = command.Parameters["@Result"].Value.ToString();
if (Result == "Employee")
{
while (dr.Read())
{
clsEmployee.EmployeeId = (int)dr["EmployeeId"];
clsEmployee.EmployeeName = (string)dr["EmployeeName"];
clsEmployee.DepartmentName = (string)dr["DepartmentName"];
clsEmployee.RoleName = (string)dr["RoleName"];
}
return clsEmployee;
}
else if (Result == "Customer")
{
while (dr.Read())
{
clsCustomer.CustomerId = (int)dr["CustomerId"];
clsCustomer.CustomerName = (string)dr["CustomerName"];
clsCustomer.CustomerEmail = (string)dr["CustomerEmail"];
clsCustomer.CustomerMobile = (string)dr["CustomerMobile"];
}
return clsCustomer;
}
//How to return???
}
When i try to return inside the condition it throws me
Not all code path return value' error.
Any solutions?