Question

I have defined the following function which would return 3 columns from table.

public DataSet GetFunc()
    {
        int iRet = 0;
        DataSet ds = new DataSet();
        SqlConnection sqlConnection = new SqlConnection();
        try
        {
            iRet = connect(ref sqlConnection);
            if (DB_SUCCESS_CONNECT == iRet)
            {
                SqlCommand sqlCommand = new SqlCommand("", sqlConnection);                   
                String strQuery = "Select ID, Did, FirstName from Users";
                sqlCommand.CommandText = strQuery;

                SqlDataAdapter adaptor = new SqlDataAdapter(sqlCommand);
                adaptor.Fill(ds);
                sqlConnection.Close();
                return ds;                    
            }

        }
        catch (Exception e)
        {
            disconnect(ref sqlConnection);
        }
    }

But when I'm trying to build it, I'm getting the error:

Error 172 'GetFunc()': not all code paths return a value

I'm confused on where am I going wrong. Can someone guide me through?

Was it helpful?

Solution 2

public DataSet GetFunc()
{
    int iRet = 0;
    DataSet ds = new DataSet();
    SqlConnection sqlConnection = new SqlConnection();
    try
    {
        iRet = connect(ref sqlConnection);
        if (DB_SUCCESS_CONNECT == iRet)
        {
            SqlCommand sqlCommand = new SqlCommand("", sqlConnection);                   
            String strQuery = "Select ID, Did, FirstName from Users";
            sqlCommand.CommandText = strQuery;

            SqlDataAdapter adaptor = new SqlDataAdapter(sqlCommand);
            adaptor.Fill(ds);
            sqlConnection.Close();
            return ds;                    
        }

    }
    catch (Exception e)
    {
        disconnect(ref sqlConnection);
    }
    return null;
}

Not all paths of your code return value, but have to. If DB_SUCCESS_CONNECT!=iRet you won't return result. Try returning some default value, maybe null like above. Another problem is that you are not returning value if exception is thrown. When exception is thrown you are disconnecting and not returning any value.

OTHER TIPS

In the try block You have given a return type while in the catch block there is no return type. This error usually occurs when the compiler does not find an appropriate return values. Try returning ds in catch

But make sure that further in your logic you check ds for a null check

it is because there is no return path in case :- DB_SUCCESS_CONNECT != iRet

If an exception is being thrown within the try ... catch block there is no return value specified.

Add:

return ds;

at the end of your function after the catch block.

You have only return statements in try block which is not assured that it will always executed due to exception what compiler assumes. Add another return statement that returns null or dataset out of try then you wont get error. You can have only one return statement instead of two or three.

public DataSet GetFunc()
{
    int iRet = 0;
    DataSet ds = null;
    SqlConnection sqlConnection = new SqlConnection();
    try
    {
        iRet = connect(ref sqlConnection);
        if (DB_SUCCESS_CONNECT == iRet)
        {
            SqlCommand sqlCommand = new SqlCommand("", sqlConnection);                   
            String strQuery = "Select ID, Did, FirstName from Users";
            sqlCommand.CommandText = strQuery;

            SqlDataAdapter adaptor = new SqlDataAdapter(sqlCommand);
            ds = new DataSet();
            adaptor.Fill(ds);
            sqlConnection.Close();                
        }         
    }
    catch (Exception e)
    {
        disconnect(ref sqlConnection);
    }
   return ds;
}

your code fail because you only return value if the condition true. if condition fail or if some exception happen there is nothing returning from your method.

and also please note, you are not handling connection properly. You need to close or dispose connection object. I would change your method as below

public DataSet GetFunc()
{
    string strQuery = "Select ID, Did, FirstName from Users";
    DataSet ds = new DataSet();
    using (var sqlConnection = new SqlConnection())
    using (var sqlCommand = new SqlCommand(strQuery, sqlConnection))
    using (var adaptor = new SqlDataAdapter(sqlCommand))
    {
        adaptor.Fill(ds);
    }
    return ds;
}

Place return statement after try and catch block, try with the code below:

public DataSet GetFunc()
{
    int iRet = 0;
    DataSet ds = new DataSet();
    SqlConnection sqlConnection = new SqlConnection();
    try
    {
        iRet = connect(ref sqlConnection);
        if (DB_SUCCESS_CONNECT == iRet)
        {
            SqlCommand sqlCommand = new SqlCommand("", sqlConnection);                   
            String strQuery = "Select ID, Did, FirstName from Users";
            sqlCommand.CommandText = strQuery;

            SqlDataAdapter adaptor = new SqlDataAdapter(sqlCommand);
            adaptor.Fill(ds);
            sqlConnection.Close();                    
        }
    }
    catch (Exception e)
    {
        disconnect(ref sqlConnection);
    }
   return ds;
}

If function has return type it should return something in all the situation, so function should have return value for Try block as well as for Catch block

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top