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.