|
-
Aug 3rd, 2012, 04:44 PM
#1
Thread Starter
Addicted Member
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!
-
Aug 3rd, 2012, 04:57 PM
#2
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.
-
Aug 4th, 2012, 12:17 AM
#3
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".
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 6th, 2012, 09:09 AM
#4
Thread Starter
Addicted Member
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.
-
Aug 6th, 2012, 09:35 AM
#5
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').
-
Aug 6th, 2012, 09:41 AM
#6
Thread Starter
Addicted Member
Re: Function giving me "not all code paths return a value"
so I can just put "return null;" in my catch?
-
Aug 6th, 2012, 10:08 AM
#7
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.
-
Aug 6th, 2012, 10:16 AM
#8
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.
Last edited by JuggaloBrotha; Aug 6th, 2012 at 10:22 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|