I have set up a Dictionary
that calls on a class to fill a DataGridView
via SQL statements. The problem is in two (out of 5) instances the value passed HAS to be an integer, but the value comes from a textbox so it is being passed as string. Before I set these Dictionaries up I was just using a switch statement that can be seen here. In my switch statement I simply added:
int n;
bool isNumber = int.TryParse(textBox.Text, out n);
if (!isNumber)
{
MessageBox.Show("Input must be an integer");
break;
}
to the two cases that needed the value to be an integer type.
Any idea on how I can add error handling to this? Preferably a message box that just says "value needs to be an integer".
private void findScriptsQueryButton_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection();
string value = this.valueTextBox.Text;
connection.ConnectionString = GetSqlConnection[serverComboBox.SelectedItem.ToString()];
findScriptsDataGrid.DataSource = GetDataSource[findByComboBox.SelectedItem.ToString()](connection, value).Tables[0];
}
private static TableAdapters.FindScript dataSource = new TableAdapters.FindScript();
private static readonly Dictionary<String, Func<SqlConnection, String, DataSet>> GetDataSource = new Dictionary<String, Func<SqlConnection, String, DataSet>>()
{
{"Target VDN", dataSource.FillByTargetVDN},
{"Skill Group", dataSource.FillBySkillGroup},
{"Translation Route Pool", dataSource.FillByTranslationRoutePool},
{"Name", dataSource.FillByName},
{"Label", dataSource.FillByLabel}
};
private static readonly Dictionary<String, String> GetSqlConnection = new Dictionary<String, String>()
{
{"SERVER01", ConfigurationManager.ConnectionStrings["csS01"].ConnectionString},
{"SERVER02", ConfigurationManager.ConnectionStrings["csS02"].ConnectionString}
};