My interface-
I am trying to expand my nested sub query-
select * from jobs where (location='delhi' or location='Mumbai') and profile in(select profile from jobs where profile='CompScience');
where for each of the check box ticked I want to add that to the condition. For example if the ticked boxes are delhi,Mumbai,CompScience
the query would be-
select * from jobs where (location='delhi' or location='Mumbai') and profile in(select profile from jobs where profile='CompScience');
this is my attempt-
private void button1_Click(object sender, EventArgs e)
{
String location=null;
string profile-null;
if (checkBox1.Checked == true)
{
location+= checkBox1.Text;
}
if (checkBox2.Checked == true)
{
location += checkBox2.Text;
}
if (checkBox3.Checked == true)
{
location += checkBox3.Text;
}
if (checkBox4.Checked == true)
{
profile += checkBox4.Text;
}
if (checkBox5.Checked == true)
{
profile += checkBox5.Text;
}
if (checkBox6.Checked == true)
{
profile += checkBox6.Text;
}
//MessageBox.Show(location);
db_CONNECT();
conn.Open();
try
{
String query = "select * from jobs where(location= 'delhi' or location = 'Mumbai') and profile in(select profile from jobs where profile = 'CompScience');";
OracleCommand comm2 = new OracleCommand(selectquery, conn);
OracleDataAdapter MyAdapter = new OracleDataAdapter();//adapter acts as interface btw database and dataset(which is collectio of tables)
MyAdapter.SelectCommand = comm2;
DataTable dTable = new DataTable();//datatable represents a single table in database
MyAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}
I tried concatenating the strings and then taking the individual elements from it.
edit-
private void button1_Click(object sender, EventArgs e)
{
db_CONNECT();
try
{
CheckBox[] Locations = { checkBox1, checkBox2, checkBox3 };
CheckBox[] Profiles = { checkBox4, checkBox5, checkBox6 };
string locs = string.Join(" or ", Locations.Where(c => c.Checked).Select(x => $"location = '{x.Text}'"));
string profs = string.Join(" or ", Profiles.Where(c => c.Checked).Select(x => $"profile = '{x.Text}'"));
string query = $"select * from jobs where ({locs}) and profile in(select profile from jobs where {profs})";
OracleCommand comm2 = new OracleCommand(query, conn);
OracleDataAdapter MyAdapter = new OracleDataAdapter();//adapter acts as interface btw database and dataset(which is collectio of tables)
MyAdapter.SelectCommand = comm2;
DataTable dTable = new DataTable();//datatable represents a single table in database
MyAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}