0

I am working on an online shopping project.......

I want to retrive data from database using checkbox......

For Example....
i have an mobbile form..... having all mobile records and have some filters such as search by brand, search by range etc....
let me take example of search by brand ......

In search by brand have 4 checkbox named Nokia(Checkbox1), Samsung(Checkbox2), Sony Erricson(Checkbox3) and Blackberry(Checkbox4).... such that if user selects Nokia all nokia mobiles will be shown to him... now problem is that if user selects two checkboxes such as nokia and samsung.... he cant see both he can see only one brands mobile either nokia or samsung.... please help...... mY code as follows..... Checkbox auto post back property set to true....

public partial class Mobiles : System.Web.UI.Page
{
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=HP-HP;Initial Catalog=OnlineShop;Integrated Security=True";
        con.Open();
        SqlCommand cmd = new SqlCommand("Select Product_Code, Name,Brand,Price,Image from mobiles", con);
        SqlDataReader dr = cmd.ExecuteReader();
        dt = new DataTable();
        dt.Load(dr);
        DataList1.DataSourceID = null;
        DataList1.DataSource = dt;
        DataList1.DataBind();

    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

        if (CheckBox1.Checked == true)
        {
            DataView dv = dt.DefaultView;
            dv.RowFilter = "Brand='Nokia'";
            DataList1.DataSource = dv;
            DataList1.DataBind();
        }




    }
    protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
    {

        if (CheckBox4.Checked == true)
        {
            DataView dv = dt.DefaultView;
            dv.RowFilter = "Brand='Blackberry'";
            DataList1.DataSource = dv;
            DataList1.DataBind();
        }

    }

    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {

        if (CheckBox2.Checked == true)
        {
            DataView dv = dt.DefaultView;
            dv.RowFilter = "Brand='Sony Ericson'";
            DataList1.DataSource = dv;
            DataList1.DataBind();
        }

    }
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {

        if (CheckBox3.Checked == true)
        {
            DataView dv = dt.DefaultView;
            dv.RowFilter = "Brand='Samsung'";
            DataList1.DataSource = dv;
            DataList1.DataBind();
        }

    }
}
2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by crazydevelopervish
1

You need to alter your filter so it includes multiple statements. I.e. "Brand='Samsung' AND brand = 'Nokia'".
To do this you will need to collate which checkboxes are checked and build up a string that represents that and pass it into your RowFilter.

private void filterView() {
   ArrayList filters = new ArrayList();
   if(Checkbox1.Checked) {
      filters.Add("brand = 'Nokia'");
   }
   if(Checkbox2.Checked) {
      filters.Add("brand = 'Samsung'");
   }
   if(Checkbox3.Checked) {
      filters.Add("brand = 'Blackberry'");
   }
   if(Checkbox4.Checked) {
      filters.Add("brand = 'SonyEricson'");
   }
   string filter = "";
   if(filters.count == 1) {
      filter = filters[0].ToString();
   } else {
      for(int i = 0; i < filters.count-1; i++) {
        filter += filters[i].ToString() + " AND ";
      }
      filter += filters[filters.count - 1].ToString();
   }
   DataView dv = dt.DefaultView;
   dv.RowFilter = filter;
   DataList1.DataSource = dv;
   DataList1.DataBind();
}

Call this function from within every CheckBox_Changed event and it should build up the string correctly. I just typed this on the fly so I'm hoping it will work (it should) but its enough for you to get the idea anyway.

Votes + Comments
0

its not working properly if i select nokia it shows all details but if i select both example nokia and samsung on the same page it dont show any records neither nokia nor samsung

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.