Click here to Skip to main content
15,886,756 members
Please Sign up or sign in to vote.
4.14/5 (4 votes)
inside of a db table's column different names of categories
e.g.,
Category
HP
Lenova
DELL
APPLE


now on a web form i have placed a check boxes list for multiple category names. Search is base on different categories(above mentioned) i am using
C#
if else
but the problem is how to write the conditions or Condtion for multiple search ??
i am doing like this
C#
if(Checkboxeslist.selectedIndex==0)//for hp
{
sql query
}
if(Checkboxeslist.selectedIndex==1)//for Dell
{
sql query
}
if(Checkboxeslist.selectedIndex==1 && Checkboxeslist.selectedIndex==0)//for Dell
{
sql query
}

and so on...conditions will never end i mean too much conditions.
please help me how to resolve this dilemma
Posted
Comments
Dholakiya Ankit 1-Aug-13 2:44am    
oh ! very confused solution 1 is ok for you

Why dont you try this

Loop through the selected item and form a dynamic query and finally execute it to get your desired output.

C#
string SQLQuery = "select blah, blah from Category where 1=1"
foreach (var item in checkedListBox1.CheckedItems)
{
	SQLQuery = SQLQuery + " or CategoryName= " + item                
}

//Execute your query


Hope it helps
 
Share this answer
 
v4
Hi,

First of all get list of all selected checkbox values.

C#
string CategoryName = string.Empty;
string query = "select * from Category";
foreach(ListItem item in checkedListBox1.Items)
{
    if(item.Selected)
    {
        CategoryName += item.Text + ",";
    }
}
if (!string.IsNullOrEmpty(CategoryName))
{
    //remove last ,
    CategoryName = CategoryName.Remove(CategoryName.Length - 1, 1);

    // now complete your query
    query += "where CategoryName in (" + CategoryName + ")";

    // now you sqlexecute code
}
 
Share this answer
 
You can try in SQL query like

SQL
Select * from Category where PK_CATID is not null (Which is always true)

Now concatenate with this Checkboxeslist.selectedValue

sql="Select * from Category where PK_CATID is not null"
sql=sql+ and CategoryName='+Checkboxeslist.selectedValue
 
Share this answer
 
v3
Comments
Adarsh chauhan 1-Aug-13 2:45am    
I think it should be
sql="Select * from Category where PK_CATID is not null"
sql=sql+ 'and CategoryName='+Checkboxeslist.selectedValue

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900