Click here to Skip to main content
15,121,414 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have made a form for searching purpose.
where
1. eight Combobx with 8 Values/Items (table Column Names)
2.
one TextBox
3.
another Combobx with two option (AND/OR)

the above 3 line group is eight time in my forms.

i have also place a checklist box with six option
now i am going to make searching query.
here is the query
C#
string SearchQuery = "Select * From Hadiths_old Where Baab_Id < 100 =" + BookSelection  + AndOrQuery ;


here BookSelection Query Fragment Formation, this is one the basis of checklist box item selection
C#
string BookSelection = "";
            if (clbBooksName.GetItemChecked(0) == true)
            {
                BookSelection = "OR Hadith_Book_Number = 1";
            }
            if (clbBooksName.GetItemChecked(1) == true)
            {
                BookSelection = "OR Hadith_Book_Number = 2";
            }
            if (clbBooksName.GetItemChecked(2) == true)
            {
                BookSelection = "OR Hadith_Book_Number = 3";
            }
            if (clbBooksName.GetItemChecked(3) == true)
            {
                BookSelection = "OR Hadith_Book_Number = 4";
            }
            if (clbBooksName.GetItemChecked(4) == true)
            {
                BookSelection = "OR Hadith_Book_Number = 5";
            }
            if (clbBooksName.GetItemChecked(5) == true)
            {
                BookSelection = "OR Hadith_Book_Number = 6";
            }


Now for Combox of Column and AND/OR Operator. I am really stuck
8 combobox values with one text box and then also another combobox with two values (and/or)
i try to make this query

C#
if(!String.IsNullOrEmpty(tbSearch1.Text) && cbColumn1.SelectedIndex ==0 && cbOperator1.SelectedIndex==0)
          {
              AndOrQuery = "AND Hadith_Arabic ="+ tbSearch1.Text + "AND";  
          }


may be this will work but i am really worry if i use this techniques then i need to make lot of condition maybe 50 or 100 condition which is not possible. please help here.
how to form query??
Posted

Your code is a nightmare. For a start, it's using ifs instead of a switch. Second, string mashing SQL is always bad. If you stop and look at your code, you are looking for booknumber n+1 where n is the checked item. That logic is pretty easy to work out in a loop, creating a list of items to look for, which you can pass in to a stored proc as XML.
   
Hi,

It seems very complex requirement here but don't worry I would like to say create a stored procedure (as you are using SQL command here) add all the parameter in that query as optional
and then in stored procedure according to value add the condition. I think that will be more easy.

Thanks
Raj
   
Comments
Muhamad Faizan Khan 11-Nov-13 23:59pm
   
what will i do with and or? operator
Rajat-Indiandotnet 12-Nov-13 1:44am
   
You can handle those in stored procedure that would be easy.

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