Question

Error :'WinWithStudentDatabase.Broker.FillComboBox()': not all code paths return a value.

I know what that error means but can not figure out why its not working :/ ... this is my code:

 public List<Person> FillComboBox()
    {
        List<Person> personsList = new List<Person>();
        try
        {
            string sql = "SELECT * FROM Tperson";
            cmd = new SqlCommand(sql, connection);
            connection.Open();

            System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != null)
            {
                Person p = new Person();

                p.Id = Convert.ToInt32(reader["ID"].ToString());
                p.FirstName = reader["FirstName"].ToString();
                p.LastName = reader["LastName"].ToString();

                personsList.Add(p);
            }
            return personsList;
        }
        catch (Exception eX)
        {
            MessageBox.Show(eX.Message);
        }
        finally
        {
            if (connection != null)
            {
                connection.Close();
            }
        }
    }

Any suggestions? im trying just to read data from my DB and fill combobox thats all..

Was it helpful?

Solution

You have declared your function to return a List<Person> but the catch block exits without returning anything

    catch (Exception eX)
    {
        MessageBox.Show(eX.Message);
        return null;

        // or return an empty list if more appropriate 
        // return new List<Person>();
    }

The compiler sees that you have coded a catch block meaning that you expect to handle an exception here. But when the catch block exits there should be a return value.
It is difficult to say what is the proper way to return in these situations. I personally prefer to return a null object and test for this in the calling code. (I avoid to return an eventually partially filled list declared at the start of the method)

OTHER TIPS

A method that returns a value must have a return statement in all code paths.

In compile time, your program can't know your catch block will work or not. That's why you should add a return value inside your catch block also.

One solution could be;

catch (Exception eX)
{
    MessageBox.Show(eX.Message);
    return null;
}

For more information, take a look at Methods (C# Programming Guide)

Problem : if you are using return statement it should be able to return the values from all the code blocks.

Solution 1: so you need to either add it in catch block or at the end of the function.

remove the return statement return personsList; from try block and add it athe end of the function

Try This:

    catch (Exception eX)
    {
        personsList = null;
    }
    finally
    {
        if (connection != null)
        {
            connection.Close();
        }

    }
 return personsList;

OR

Add the return statement both in try and catch block

 catch (Exception eX)
    {
        MessageBox.Show(eX.Message);
        return null;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top