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!
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.
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".
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.
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').
Re: Function giving me "not all code paths return a value"
so I can just put "return null;" in my catch?
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.
Re: Function giving me "not all code paths return a value"
c# Code:
public static BindingSource GetAllRecords(string table)
{
System.Windows.Forms.BindingSource oBindingSource = null;
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;
}
cmdGetRecords.CommandText = sSQL;
cmdGetRecords.CommandType = CommandType.Text;
try
{
HandleConnection(cnGetRecords);
oBindingSource = GetBindingSource(cmdGetRecords);
if (oBindingSource == null)
{
throw new Exception("There was no BindingSource returned");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (cnGetRecords.State <> ConnectionState.Closed) { cnGetRecords.Close(); }
cmdGetRecords.Dispose();
cnGetRecords.Dispose();
}
//If either of the two exceptions are thrown, this line will not be reached anyways
return oBindingSource;
}
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.