How can I search only checked items from database [ASP.NET]

I got a table (cake_client_table) with 8 columns (id, sensitivity , service_price, service_quality…) and many rows (Example: 0001, yes, luxurious, high quality, classy…). I have 8 checkboxes which contains one data from each rows. Now I want to search from my another table (cake_org_table) where those checkbox values are similar with this table’s column values. For example, if ‘yes’ and ‘classy’ are checked, then it will search and display all rows with sensitivity ‘yes’ and wed_cake ‘classy’. First it stores each text of the checkboxes into a textbox. That means I got 8 textboxes. Then I wrote a code to search the data on the textbox if the checkbox is checked. But if it isn’t checked, assign null value on the textbox. Here is the code:

 string chkbox8 = "";
    
             for (int i = 0; i < CheckBoxList8.Items.Count; i++)
             {
                 if (CheckBoxList8.Items[i].Selected)
                 {
    
                     chkbox8 = CheckBoxList8.Items[i].Text;
    
                 }
                 else
                 {
                     chkbox8 = null;
                 }
                 TextBox1.Text = chkbox8;
    
             }

But the issue is when the checkbox isn’t checked and the textbox value is null, it searches for null value on the database. And then since there is no null value, it displays nothing. But I need it to just search only the checked items (for example ‘yes’ and ‘classy’) and doesn’t search for the empty parts i.e. the unchecked 6 checkboxes. But it searches for ‘yes’, ‘classy’ and 6 null values which I don’t want. Here is the code that searches it from the database.

try
            {

                SqlConnection con = new SqlConnection(strcon);
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd = new SqlCommand("SELECT * from cake_org_tbl WHERE sen_2_taste='" + TextBox1.Text + "' AND service_price = '" + TextBox2.Text + "';", con);
               DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                GridView1.DataSource = dt;
               
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "');</script>");
            }

As a user you are able to select specific columns of data that are desired for viewing. The data is then returned in the form of an HTML table. You can create views that show only checked items or by bulk operations, which include: Union, Intersect and Except.

Thanks for replying, @miamarshal I don’t have a problem with all columns being displayed. I want all of them to be displayed. My issue is I don’t want it to search ‘null’ value from the columns. When the checkbox is not checked, instead of searching a null value from a column, I want it not to search at all and display rows with the checked item values only.

Here is an example:

if ‘classy’ and ‘yes’ are checked and ‘tasty’ not checked:

I want all columns with ‘classy’ and ‘yes’ values to be displayed. But ‘tasty’ isn’t part of the measurement. But if ‘tasty’ is checked, I want all the three attributes to be part of the measurement. But now what the system doing is it searches for ‘classy’, ‘yes’ and ‘null’.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.