Results 1 to 8 of 8

Thread: Function giving me "not all code paths return a value"

  1. #1
    Addicted Member
    Join Date
    Jan 10
    Location
    San Marcos, TX
    Posts
    177

    Function giving me "not all code paths return a value"

    Here's my function:

    Code:
            public static BindingSource GetAllRecords(string table)
            {
                string sSQL = null;
                System.Data.SqlClient.SqlConnection cnGetRecords = new SqlConnection(DataMSSQL.GetConnectionString("MSSQLConnection"));
                System.Data.SqlClient.SqlCommand cmdGetRecords = new SqlCommand();
    
                switch (table)
                {
                    case "clients":
                        sSQL = "SELECT * FROM vw_AlphaClients";
                        break;
                    case "counties":
                        sSQL = "SELECT * FROM vw_AlphaCounties";
                        break;
                    case "employees":
                        sSQL = "SELECT * FROM vw_AlphaEmployees";
                        break;
                    case "jobs":
                        sSQL = "SELECT * FROM vw_NumJobs";
                        break;
                    case "states":
                        sSQL = "SELECT * FROM vw_AlphaStates";
                        break;
                    case "subdivisions":
                        sSQL = "SELECT * FROM vw_AlphaSubdivisions";
                        break;
                    case "surveys":
                        sSQL = "SELECT * FROM vw_AlphaSurveys";
                        break;
                    case "surveysCounties":
                        sSQL = "SELECT * FROM vw_AlphaSurveysCounties";
                        break;
                    case "types":
                        sSQL = "SELECT * FROM vw_AlphaTypes";
                        break;
                    case "units":
                        sSQL = "SELECT * FROM vw_AlphaUnits";
                        break;
                }
                //string sSQL = "GetAllRecords";
                SqlDataAdapter daGetRecords;
                DataTable dtGetRecords;
                cmdGetRecords.CommandText = sSQL;
                cmdGetRecords.CommandType = CommandType.Text;
                try
                {
                    HandleConnection(cnGetRecords);
                    System.Windows.Forms.BindingSource oBindingSource = GetBindingSource(cmdGetRecords);
                    if (oBindingSource != null)
                    {
                        return oBindingSource;
                    }
                    else
                    {
                        throw new Exception("There was no BindingSource returned");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                cnGetRecords.Close();
            }
    How do I make this error go away? It is returning oBindingsource if all goes well. Is this just a warning or an actual error that prevents me from building?

    Thanks!

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,984

    Re: Function giving me "not all code paths return a value"

    It's an actual error. A function must return a value. This only returns one if the condition is true, not when it is false. Either make it a sub and return the value via a global variable or return the null and respond to it in your calling sub.

  3. #3
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    605

    Re: Function giving me "not all code paths return a value"

    the problem is that you're not returning a BindingSource upon all possible conditional outcomes based on the logic within your function. If the conditions aren't met for places where you return a BindingSource, then there is no BindingSource returned, therefore, "not all code paths return a value".
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  4. #4
    Addicted Member
    Join Date
    Jan 10
    Location
    San Marcos, TX
    Posts
    177

    Re: Function giving me "not all code paths return a value"

    Would changing the else in the try/catch to this:

    return null;
    throw new Exception("There was no BindingSource returned");

    not fix the issue? I've added that code and it's still giving me the same error.

  5. #5
    Fanatic Member
    Join Date
    Jan 06
    Posts
    515

    Re: Function giving me "not all code paths return a value"

    You don't need a return there - adding it before the 'throw' would mean that the 'throw' never executes.
    You need a return statement in the 'catch' (or after the 'catch').
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  6. #6
    Addicted Member
    Join Date
    Jan 10
    Location
    San Marcos, TX
    Posts
    177

    Re: Function giving me "not all code paths return a value"

    so I can just put "return null;" in my catch?

  7. #7
    Fanatic Member
    Join Date
    Jan 06
    Posts
    515

    Re: Function giving me "not all code paths return a value"

    Yes - and you probably want to add a 'finally' block and close the SqlConnection object there instead.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 05
    Location
    Lansing, MI; USA
    Posts
    3,788

    Re: Function giving me "not all code paths return a value"

    c# Code:
    1. public static BindingSource GetAllRecords(string table)
    2. {
    3.     System.Windows.Forms.BindingSource oBindingSource = null;
    4.     string sSQL = null;
    5.     System.Data.SqlClient.SqlConnection cnGetRecords = new SqlConnection(DataMSSQL.GetConnectionString("MSSQLConnection"));
    6.     System.Data.SqlClient.SqlCommand cmdGetRecords = new SqlCommand();
    7.  
    8.     switch (table)
    9.     {
    10.     case "clients":
    11.         sSQL = "SELECT * FROM vw_AlphaClients";
    12.         break;
    13.     case "counties":
    14.         sSQL = "SELECT * FROM vw_AlphaCounties";
    15.         break;
    16.     case "employees":
    17.         sSQL = "SELECT * FROM vw_AlphaEmployees";
    18.         break;
    19.     case "jobs":
    20.         sSQL = "SELECT * FROM vw_NumJobs";
    21.         break;
    22.     case "states":
    23.         sSQL = "SELECT * FROM vw_AlphaStates";
    24.         break;
    25.     case "subdivisions":
    26.         sSQL = "SELECT * FROM vw_AlphaSubdivisions";
    27.         break;
    28.     case "surveys":
    29.         sSQL = "SELECT * FROM vw_AlphaSurveys";
    30.         break;
    31.     case "surveysCounties":
    32.         sSQL = "SELECT * FROM vw_AlphaSurveysCounties";
    33.         break;
    34.     case "types":
    35.         sSQL = "SELECT * FROM vw_AlphaTypes";
    36.         break;
    37.     case "units":
    38.         sSQL = "SELECT * FROM vw_AlphaUnits";
    39.         break;
    40.     }
    41.    
    42.     cmdGetRecords.CommandText = sSQL;
    43.     cmdGetRecords.CommandType = CommandType.Text;
    44.     try
    45.     {
    46.         HandleConnection(cnGetRecords);
    47.         oBindingSource = GetBindingSource(cmdGetRecords);
    48.         if (oBindingSource == null)
    49.         {
    50.             throw new Exception("There was no BindingSource returned");
    51.         }
    52.     }
    53.     catch (Exception ex)
    54.     {
    55.         MessageBox.Show(ex.Message);
    56.     }
    57.     finally
    58.     {
    59.         if (cnGetRecords.State <> ConnectionState.Closed) { cnGetRecords.Close(); }
    60.         cmdGetRecords.Dispose();
    61.         cnGetRecords.Dispose();
    62.     }
    63.    
    64.     //If either of the two exceptions are thrown, this line will not be reached anyways
    65.     return oBindingSource;
    66. }
    I would also suggest you adding a case default to the bottom in case none of those cases are true. That way you could throw an exception stating a valid table name is required.
    Last edited by JuggaloBrotha; Aug 6th, 2012 at 10:22 AM.
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.



    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •