-1

I am getting an error when I write a method returning a datatable using if and else if statement. what is the correct format. Thanks in advance "not all code paths return a value"

    [HttpGet]
    public DataTable getcareerdata (int tclass, int efkey)
    {
        if (tclass ==7)
        {
            return obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree, case When Scientific_Degree  is null then 'Not Related'
            when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else latin_desc  end as Spec from hr_effect_classes Left Outer Join general_cod 
            on hr_effect_classes.Scientific_Degree = general_cod .sub_cod and  main_cod = 17001  where type_class in (7) and status_class = 1  and effect_key = {0}
            Order By  Scientific_Degree  , From_Value ", efkey));
        }
        else if (tclass == 8)
        {
            return obj.GetData(string.Format(@"select Effect_Class_Key ,  From_Value , To_Value , Effect_Class_Value, Scientific_Degree case When Scientific_Degree is null then 'Not Related'
            when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else L_Desc end as Spec from hr_effect_classes Left Outer Join HR_Specialty   
            on hr_effect_classes.Scientific_Degree = HR_Specialty.Spec_Key where type_class in (8) and status_class = 1 and effect_key = {0}
            Order By  Scientific_Degree  , From_Value ",efkey));
        }
    }
hassanzi
  • 191
  • 17
  • 2
    In the case where tclass doesn't = 7 or 8, there is no datatable returned. How you handle that depends on what you need. – Sudsy1002 Apr 13 '18 at 17:58
  • do you need a catch-all `else` ? – Scott Weaver Apr 13 '18 at 17:58
  • 1
    You can add a `return null` at the end to avoid that compile error. – Rufus L Apr 13 '18 at 17:59
  • declare a DataTable at the top of your class (outside the if), and then populate that Datatable in the if statement, and do the RETURN outside the if at the bottom. – Brad Apr 13 '18 at 17:59
  • I think that you can find thousands of duplicates for this error. Just try to search before posting next time – Steve Apr 13 '18 at 18:01

2 Answers2

2

if your both if conditions fail then your method has nothing to return while your method signatures telling the compiler that it will return an object of type DataTable which it would fail to do so in the case which is stated above.

So, What should be done is use a local variable of type DataTable and set it's value accordingly in the conditional blocks and then finally return the variable back from method.

Refactor your method to be like :

DataTable table = null;
if (tclass ==7)
{
    table  = obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree, case When Scientific_Degree  is null then 'Not Related'
            when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else latin_desc  end as Spec from hr_effect_classes Left Outer Join general_cod 
            on hr_effect_classes.Scientific_Degree = general_cod .sub_cod and  main_cod = 17001  where type_class in (7) and status_class = 1  and effect_key = {0}
            Order By  Scientific_Degree  , From_Value ", efkey));
}
else if (tclass == 8)
{
    table  = obj.GetData(string.Format(@"select Effect_Class_Key ,  From_Value , To_Value , Effect_Class_Value, Scientific_Degree case When Scientific_Degree is null then 'Not Related'
            when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else L_Desc end as Spec from hr_effect_classes Left Outer Join HR_Specialty   
            on hr_effect_classes.Scientific_Degree = HR_Specialty.Spec_Key where type_class in (8) and status_class = 1 and effect_key = {0}
            Order By  Scientific_Degree  , From_Value ",efkey));
}

 return table;

Now we are returning a reference of type DataTable though it would be null if both conditions fail but it will make your code that compiler takes as valid without any build time errors.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
2

You need to always return something no matter what, so add return null to outside the if statement if neither condition is met.

[HttpGet]
public DataTable getcareerdata (int tclass, int efkey)
{
    if (tclass ==7)
    {
        return obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree, case When Scientific_Degree  is null then 'Not Related'
        when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else latin_desc  end as Spec from hr_effect_classes Left Outer Join general_cod 
        on hr_effect_classes.Scientific_Degree = general_cod .sub_cod and  main_cod = 17001  where type_class in (7) and status_class = 1  and effect_key = {0}
        Order By  Scientific_Degree  , From_Value ", efkey));
    }
    else if (tclass == 8)
    {
        return obj.GetData(string.Format(@"select Effect_Class_Key ,  From_Value , To_Value , Effect_Class_Value, Scientific_Degree case When Scientific_Degree is null then 'Not Related'
        when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else L_Desc end as Spec from hr_effect_classes Left Outer Join HR_Specialty   
        on hr_effect_classes.Scientific_Degree = HR_Specialty.Spec_Key where type_class in (8) and status_class = 1 and effect_key = {0}
        Order By  Scientific_Degree  , From_Value ",efkey));
    }
return null;
}
Scath
  • 3,777
  • 10
  • 29
  • 40