Question

I am generating a CheckBoxList from a sql query and all works fine, that CheckBoxList is dependent, so, when I check one only option, it generates another CheckBoxList and it works fine too. I want to check multiple options and get all data but the query runs only with the last chekbox checked.

How can I implement the query that accepts all the options checked in the CheckBoxList and display all data?

Here my code:

protected void cblList1_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (ListItem list1 in cblList1.Items)
    {
        if (list1 .Selected == true)
        {
            LoadCheckBoxListList2(list1);
        }
    }
}

private void LoadCheckBoxListList2(ListItem itemList1)
{
    SqlCommand cmd = new SqlCommand("SELECT DISTINCT tbl_information.route AS ROUTE FROM tbl_information INNER JOIN tbl_regional ON tbl_information.region = tbl_regional.id_regional WHERE tbl_information.supervisor = " + "'" + itemList1 + "'", conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    cblRutas.DataSource = ds;
    cblRutas.DataValueField = "ROUTE";
    cblRutas.DataBind();
}
Was it helpful?

Solution 3

I got the answer, here's the code:

protected void cblList1_SelectedIndexChanged(object sender, EventArgs e)
{
    List<string> list1 = new List<string>();
    foreach (ListItem itemList1 in cblList1.Items)
    {
        if (itemList1 .Selected == true)
        {
            list1.Add(itemList1.Text)
        }
    }
    LoadCheckBoxListList2(list1);
}

private void LoadCheckBoxListList2(List<string> list1)
{
    DataTable dt = new DataTable("R");
    dt.Columns.Add("Route", typeof(string));

        foreach (string li in list1)
        {
            string query = @"SELECT DISTINCT tbl_information.route AS ROUTE FROM tbl_information INNER JOIN tbl_regional ON tbl_information.region = tbl_regional.id_regional WHERE tbl_information.supervisor = '" + li + "'";

            SqlCommand cmd = new SqlCommand(query, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            bool b = false;

            foreach (DataRow item in ds.Tables[0].Rows)
            {
                if (!b)
                {
                    dt.Rows.Add(item[0] + " (" + li + ")");
                    b = true;
                    continue;
                }

                dt.Rows.Add(item.ItemArray);
            }
        }

        cblRutas.DataSource = dt;
        cblRutas.DataValueField = "ROUTE";
        cblRutas.DataBind();
    }

Thanks for your aswers!!!

OTHER TIPS

      for (int i = 0; i < checkedListBox1.Items.Count; i++)
        {
            if (checkedListBox1.GetItemChecked(i))
            {
                string str = (string)checkedListBox1.Items[i];
                //Do your function call here
            }
        }

If I got your question correctly, in your SelectedIndexChanged event handler you have to collect list of all the checked items(not only the last one) and query which has not where id=@id filter but where id in (@id1, @id2, etc).

So you just have to change your code a little bit:

protected void cblList1_SelectedIndexChanged(object sender, EventArgs e)
{
    List<string> ids = new List<string>();
    foreach (ListItem list1 in cblList1.Items)
    {
        if (list1 .Selected == true)
        {
            ids.Add(list1.ToString());            
        }
    }

    LoadCheckBoxListList2(ids);
}

private void LoadCheckBoxListList2(List<string> ids)
{
    string idsString = string.Join(", ", ids);// WARNING: carefull, SQL-injection is possible here, better to filter the input
    SqlCommand cmd = new SqlCommand("SELECT DISTINCT tbl_information.route AS ROUTE FROM tbl_information INNER JOIN tbl_regional ON tbl_information.region = tbl_regional.id_regional WHERE tbl_information.supervisor IN (" + idsString + ")", conn);

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    cblRutas.DataSource = ds;
    cblRutas.DataValueField = "ROUTE";
    cblRutas.DataBind();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top