Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear Friends,

I have written a code like this to check the access level.
But I am getting the error as "Not All Code Path Return A Value"
Kindly suggest the reason and solution for the same

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
               if (isAccessLevel() == "YES")
               {
                  Show_My_Data()
               }
               else
               {
                  Hide_My_Data()
               }
        }
   }

public string isAccessLevel()
    {
            if (ACC_YN == "YES")
            {
                if (STA_YN == "RELEASE")
                {
                    return "YES";
                }
                else
                {
                    return "NO";
                }
            }
    }
Posted

isAccessLevel is not returning the value in from all branches. Change it to like this,

SQL
public string isAccessLevel()
    {
            if (ACC_YN == "YES")
            {
                if (STA_YN == "RELEASE")
                {
                    return "YES";
                }
                else
                {
                    return "NO";
                }
            }
            else
            {
                return "NO";
            }
    }
 
Share this answer
 
You should have return variable outside of if else loop. think what will happen,if ACC_YN=false. what it will return?

C#
public string isAccessLevel()
    {
string returnvalue="No";
            if (ACC_YN == "YES")
            {
                if (STA_YN == "RELEASE")
                {
                   returnvalue="Yes";
                }
                else
                {
                    returnvalue="No";
                }
            }
return returnvalue;
    }
 
Share this answer
 
v2
Comments
Arunprasath Natarajan 20-Aug-12 7:33am    
Tan Q, Now I am Clear
The reasoning is sound above, I would suggest the following code snippets.

C#
public string isAccessLevel()
{
    accessLevel = "NO";
    if (ACC_YN == "YES" && STA_YN == "RELEASE")
        accessLevel = "YES";

    return accessLevel;
}
 
Share this answer
 
v2
Comments
Geoffrey Moir 27-Sep-14 23:04pm    
What was the reason for the vote of 1? I am happy to get feedback on my answers, so I can improve, but not if I don't get told what I am getting down voted for.
SoMad 27-Sep-14 23:09pm    
I am not the one who downvoted you, but someone might have done it just because you are answering a two year old question that already has plenty of answers.

Soren Madsen
Leo Chapiro 16-Dec-15 7:04am    
I have upvoted with +5 to compensate yout damage although I think this points mean absolutely nothing :)
You have not allowed for the case where ACC_YN != "YES", you should add another return statement at the end of the method.
 
Share this answer
 
Your method does not return a value in all cases. I would change it like this:

C#
public string isAccessLevel()
{
    string returnValue = "Default";

    if (ACC_YN == "YES")
    {
        if (STA_YN == "RELEASE")
        {
            returnValue =  "YES";
        }
        else
        {
            returnValue = "NO";
        }
    }

    return returnValue;
}
 
Share this answer
 
Its because you don't have an outer else block that returns something if ACC_YN == "NO":

if (...)
{
}
else
{
return "SOMEVALUE"
}

if the action is to return a "NO" and ACC_YN == "NO" then u you can do something like:

C#
public string isAccessLevel()
        {
            string val = "NO";

            if (ACC_YN == "YES")
            {
                if (STA_YN == "RELEASE")
                {
                    val = "YES";
                }
                else
                {
                    val = "NO";
                }
            }

            return val;
        }



Hope this helps.
 
Share this answer
 
hii,
change function as below
public string isAccessLevel()
{
string str="";
if (ACC_YN == "YES")
{
if (STA_YN == "RELEASE")
{
str="YES";
}
else
{
str="NO";
}
}
return str;
}
 
Share this answer
 
Comments
Member 12285135 1-Apr-16 1:52am    
i am facing the same problem. I am trying to import .CSV file using a webservice and wanted to display in GridView
My Web service code is:
[WebMethod]
public DataTable imoprtCSV(string path)

//for imortCSV it is giving error as (attenService1.imoprtCSV(string)':not all code paths return a value)


//code:

if(File.Exits(path))
{
StreamReader sr=new StreamReader(path);
string line =sr.ReadLine();
string[] value=sr.Spilt(',');
DataTable dt=new DataTable()
DataRow row;
foreach(string dc in values)
{
dt.columns.add(new DataColumn (dc))
}
while(!sr.EndOfStream)
{
value=sr.ReadLine.Split(',');
if(value.length==dt.Colunms.Count)
{
row=dt.NewRow;
row.ItemArray=value;
dt.Rows.add(row);
}
return dt;
}
}

//and while calling service i used
attenService1 ats=new attenService1();
//browse the csv file
string path=string.concat(Server.MapPath(~//aaa+FileUpload1.Filename);
FileUpload1.PostedFile.SaveAs(path)
GridView1.DataSource=ats.importCSV(path);
GridView1.DataBind();

please anyone solve my problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900