none
why getting not all code paths return a value RRS feed

  • Question

  • Hi,

    Why am I getting not all code paths return a value for the below code and how to fix it?

    private DateTime get_business_date()
    {
        try
        {
            sql_connection = new SqlConnection(my_connection);
            sql_connection.Open();
    
            sql_command = new SqlCommand("sp_get_business_date", sql_connection);
            sql_command.CommandType = CommandType.StoredProcedure;
            sql_reader = sql_command.ExecuteReader();
    
            if (sql_reader.Read())
            {
                if (sql_reader.HasRows)
                {
                    business_date = Convert.ToDateTime(sql_reader["category_name"];
                }
            }
    
            return business_date;
        }
        catch (Exception exp)
        {
            XtraMessageBox.Show(String.Format("Error reading business date!!{0}{1}", Environment.NewLine, exp.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            if (sql_reader != null) { sql_reader.Close(); sql_reader.Dispose(); }
            if (sql_connection != null) { if (sql_connection.State == ConnectionState.Open) { sql_connection.Close(); sql_connection.Dispose(); } }
        }
    }
    

    Thanks,

    Jassim

    Saturday, December 10, 2016 12:23 AM

All replies

  • One of the solutions is to add ‘return DateTime.MinValue;’ after the message box. This value will be the indicator of the error, therefore the callers of get_business_date will have to analyse the result using an ‘if’.

    Another solution: add ‘throw;’ after the message box.

    • Proposed as answer by Kristin Xie Monday, December 12, 2016 2:19 AM
    Saturday, December 10, 2016 6:02 PM
  • >>Why am I getting not all code paths return a value for the below code?

    Because the method has a return type of DateTime but it doesn't always return a DateTime value. For example, if your code would throw an exception when you try to connect to the database for some reason you will catch this exception and display an XtraMessageBox but you will never reach the return statement (return business_date;) and no DateTime value is then returned from the method.

    >>and how to fix it?

    Put the return statement after your catch and finally clauses:

    private DateTime get_business_date()
    {
        try
        {
            sql_connection = new SqlConnection(my_connection);
            sql_connection.Open();
    
            sql_command = new SqlCommand("sp_get_business_date", sql_connection);
            sql_command.CommandType = CommandType.StoredProcedure;
            sql_reader = sql_command.ExecuteReader();
    
            if (sql_reader.Read())
            {
                if (sql_reader.HasRows)
                {
                    business_date = Convert.ToDateTime(sql_reader["category_name"];
                }
            }
        }
        catch (Exception exp)
        {
            XtraMessageBox.Show(String.Format("Error reading business date!!{0}{1}", Environment.NewLine, exp.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            if (sql_reader != null) { sql_reader.Close(); sql_reader.Dispose(); }
            if (sql_connection != null) { if (sql_connection.State == ConnectionState.Open) { sql_connection.Close(); sql_connection.Dispose(); } }
        }
    
         return business_date;
    }
    

    Then the compiler will accept your implementation since you are always returning a (possibly default or not set if there is an exception being thrown and catched) DateTime value.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Kristin Xie Monday, December 12, 2016 2:17 AM
    Sunday, December 11, 2016 5:04 PM
  • Hi Jassim,

    Do you have any updates now? If yes, please post here. If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members find solution if they faces similar issue.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 14, 2016 1:31 AM