Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello friends
the below code give an error as all code must have return path.where can i put returnpath?

public List<string> bc11()
        {
            try
            {
                Dob.open();
                SqlCommand cmd6 = new SqlCommand("sp_bc1", Dob.con);
                cmd6.CommandType = CommandType.StoredProcedure;
                
                SqlDataReader dr = cmd6.ExecuteReader();
                List<string> bi = new List<string>();
                
                
                    while (dr.Read())
                    {
                      
                        bi.Add(dr["Schoolname"].ToString());
                        return bi;
                    }
               
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                if (Dob.con != null)
                {
                    Dob.close();
                }
            }
Posted
Comments
Sergey Alexandrovich Kryukov 26-Jul-12 17:21pm    
Why do you thing anyone can tell you where is should be exactly? It depends on your required specs.
--SA

Need to add the return after the while loop:

C#
while (dr.Read())
  {
      bi.Add(dr["Schoolname"].ToString());
  }
     return bi;


The way you have it now, the bi will be returned after the first loop of the while loop. Don't think that is what you want.
 
Share this answer
 
Comments
CodeHawkz 26-Jul-12 23:51pm    
Not everyone can express their problems in a way that others can understand. It's a skill that some does not possess. But there is a rarer kind of people who can understand the above mentioned type of people. And Clifford, I think you've given him exactly what he wanted, even though he couldn't express it.

Hats off to you for the answer and for not running him down when you could understand. 5 Points from me! Keep up the good work
ZurdoDev 27-Jul-12 7:54am    
The problem with this is if an exception occurs before bi gets set it will jump to the catch and then run finally but will never actually return bi. The other listed solutions point this out.
Clifford Nelson 27-Jul-12 12:41pm    
Sorry! I only answered the question, finding the problem for what was asked. I did not debug the whole method. He did not ask about the Finally, and in any case, if an exception is thrown, nothing will be returned anyway because and exception was thrown, so why worry. You better go back to your books.
You need a return statement after the while loop. Think about it, if for some reason dr.Read() starts as false, the code inside the loop is never hit, and the function ends without anything being returned, which is not valid except in a void function.

But why is that even in a loop at all? It leaves the function after the first iteration, so if instead of while would have the same result.
 
Share this answer
 
Not all of the paths the code could take return a value. For example, if there are no records on dr your return bi will not execute and so the function will not have a return value. That is what the message is telling you.

I prefer a single return statement per function. So, at the beginning of your function declare a new variable of type List<string> and then set it to bi in your while statement. Then return the new variable after your finally. That way, no matter what the function returns a value.
 
Share this answer
 
It's totally up to you where you put it; this is really simple: no matter how the execution goes, return statement should be reached in all cases. (We should never have undefined values returned, right?) Some people demand that there should be only one return statement in the method (except void methods, of course). I personally don't follow this style and don't consider it important; so I allow any number of return statements. By the way, you can consider design where you return null. However, you should have consistent design shared by called and calling methods. Formally, such issues are settled using preconditions and postconditions:

Please see:
http://en.wikipedia.org/wiki/Precondition[^],
http://en.wikipedia.org/wiki/Postcondition[^].

—SA
 
Share this answer
 
v2
public List<string> bc11()
{
XML
List<string> bi = new List<string>();


try
{
Dob.open();
SqlCommand cmd6 = new SqlCommand("sp_bc1", Dob.con);
cmd6.CommandType = CommandType.StoredProcedure;

SqlDataReader dr = cmd6.ExecuteReader();


while (dr.Read())
{

bi.Add(dr["Schoolname"].ToString());

}

}
catch (SqlException ex)
{
throw ex;
}
finally
{
if (Dob.con != null)
{
Dob.close();
}
return bi;

}
 
Share this answer
 

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