1

My interface-

enter image description here

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();
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
ubuntu_noob
  • 2,305
  • 6
  • 23
  • 64
  • Why don't you simply do an `location IN ()`? What query does your current code produce? The obvious problem seems to be that your generated query is missing the `'` around the values. – Filburt Nov 09 '18 at 09:50
  • Possible duplicate of [Passing multiple values to single parameter in SQL inline query using C#](https://stackoverflow.com/questions/53217051/passing-multiple-values-to-single-parameter-in-sql-inline-query-using-c-sharp) – mjwills Nov 09 '18 at 09:52

3 Answers3

1

You may have an array of your checkboxes and join checked ones texts using string.Join():

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 result = $"select * from jobs where ({locs}) and profile in(select profile from jobs where {profs})";

If you have checkboxes on a parent container, like a groupbox, or panel you can even do like this:

CheckBox[] Locations = locPanel.Controls.OfType<CheckBox>().ToArray();
CheckBox[] Profiles = profPanel.Controls.OfType<CheckBox>().ToArray();
Filburt
  • 17,626
  • 12
  • 64
  • 115
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
1
List<string> locations = new List<string>();

I would change button1_Click method as follows:

private void button1_Click(object sender, EventArgs e)
{       
    if (checkBox1.Checked == true)
    {
        locations.Add(checkBox1.Text);
    }
    else
    {
        locations.Remove(checkBox1.Text);
    }
    // and so on for other locations
}

Then you can create query command the following way (this is example just for locations, for profile you should do the the same way):

var locationsString = string.Join(", ", locations.Select(l => $"'{l}'"));  // this gives you, e.x. 'Delhi', 'Mumbai'

var query = "";
if (locations.Any())
{
    query = $"select * from jobs where(location in {locationsString }) and profile in(select profile from jobs where profile = 'CompScience');";
}
else
{
    query = $"select * from jobs where profile in(select profile from jobs where profile = 'CompScience');";
}
Dmitry Stepanov
  • 2,776
  • 8
  • 29
  • 45
1

Expanding on Dmitry's answer, I'd suggest to further simplify this to

// ... do locations AND profiles the way Dmitry suggested

// start out with a generic query
StringBuilder querybuilder = new StringBuilder("SELECT * FROM jobs WHERE 1 = 1");

if (locations.Any())
{
    var locationsString = string.Join(", ", locations.Select(l => $"'{l}'"));
    querybuilder.AppendFormat(" AND location IN ({0})", locationsString);
}

if (profiles.Any())
{
    var profilesString = string.Join(", ", profiles.Select(l => $"'{l}'"));
    querybuilder.AppendFormat(" AND profile IN ({0})", profilesString);
}

// ...

OracleCommand comm2 = new OracleCommand(querybuilder.ToString(), conn);

The catch-all WHERE 1 = 1 is the commonly accepted way to create dynamic composed queries allowing you to greatly simplify the conditions for appending variable clauses to your query.

Filburt
  • 17,626
  • 12
  • 64
  • 115