Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All ,

I am trying to write the advanced search query , that allow the user to enter as many filters as he/she wish ?
This is the query that i use , the problem is that it require the user to enter all filter to work ,i need it to accept any collection of filters ,


I appreciate your help!

String q="SELECT [user].fname, [user].mname, [user].lname, ";
                   q+="[user].email, [user].mobile_num, [user].user_name, doctor_job_history.date, ";
                   q+="doctor_job_history.operation_id, entity.name AS entity_name, country.name AS con_name, ";
                   q+="degree.degree, working_hours.day, working_hours.time_from, working_hours.time_until, educaation.description, ";
                   q+="doctor_job_history.entity_id ";
                   q += "FROM [user] ,doctor_info,educaation,";
                   q += "doctor_job_history ,working_hours,degree,country,entity ";

                   q += "WHERE ([user].id = doctor_info.user_id) ";
                   q +="AND(educaation.reg_num = doctor_info.reg_num) ";
                   q +="AND(doctor_info.reg_num = working_hours.reg_num) ";
                   q +="AND(doctor_info.reg_num = doctor_job_history.reg_num) ";
                   q +="AND(degree.id=educaation.degree_id) ";
                   q += "AND(working_hours.entity_id=entity.id) ";
                   q += "AND(degree.id=educaation.degree_id) ";
                  q += "AND(country.country_id = entity.country_id) ";
                   //q += "AND(entity.id=working_hours.entity_id) ";
                  q += "AND([user].fname LIKE @fname or @fname = null)AND ([user].lname LIKE @lname or @lname = null) AND (educaation.degree_id = @degree_id or @degree_id = null) AND (educaation.univ_id = @univ_id or @univ_id = null) AND (educaation.description LIKE @description or @description = null) AND (working_hours.day = @day or @day = null) AND (working_hours.time_from = @time_from or @time_from = null) AND (working_hours.time_until = @time_until or @time_until = null) AND (working_hours.entity_id = @entity_id or @entity_id i= null)";
                
            SqlCommand comm = new SqlCommand(q, conn);
            comm.Parameters.Add("@fname", System.Data.SqlDbType.NVarChar);
            comm.Parameters.Add("@lname", System.Data.SqlDbType.NVarChar);
            comm.Parameters.Add("@degree_id", System.Data.SqlDbType.Int);
            comm.Parameters.Add("@description", System.Data.SqlDbType.NVarChar);
            comm.Parameters.Add("@day", System.Data.SqlDbType.NVarChar);
            comm.Parameters.Add("@time_from", System.Data.SqlDbType.Time);
            comm.Parameters.Add("@time_until", System.Data.SqlDbType.Time);
           comm.Parameters.Add("@entity_id", System.Data.SqlDbType.Int);
            comm.Parameters.Add("@univ_id", System.Data.SqlDbType.Int);
            comm.Parameters["@fname"].Value =Txtname .Text ;
            comm.Parameters["@lname"].Value = Txtlname .Text ;
            comm.Parameters["@degree_id"].Value =ddldegree .SelectedValue;
            comm.Parameters["@description"].Value =Txtdescription .Text ;
            comm.Parameters["@day"].Value = ddlday.SelectedValue;
            comm.Parameters["@time_from"].Value = ddltimef.SelectedValue;
            comm.Parameters["@time_until"].Value = ddltimentil.SelectedValue;
           comm.Parameters["@entity_id"].Value = ddlentity.SelectedValue;
            comm.Parameters["@univ_id"].Value = ddlniv.SelectedValue;


Please help me in this.

Thanks in Advance.
Posted
Updated 20-May-11 23:26pm
v3

You can dynamically create the query like this

...
                   if(doctor_info.reg_num != ""){
                      q +="AND(educaation.reg_num = doctor_info.reg_num) ";
                   }
...


OR

You can set up the where with LIKE instead of =

...
                   q += "WHERE ([user].id LIKE '%' + doctor_info.user_id + '%') ";
...


The like returns any results that contain the string in between the % signs

That way if they leave one field blank it searches for %% or anything
 
Share this answer
 
Comments
hebaSaleh 29-May-11 6:40am    
Great idea , we used to think about "like" ideally , in this context it gives a great help !!
thaaaanx alot
sravani.v 10-Apr-12 6:10am    
My 5!
A similar question[^] was asked earlier - maybe answers from that can help you.
 
Share this answer
 
Comments
hebaSaleh 21-May-11 9:02am    
thank you so much, but the solution provided there wont work with me , i'll have up to 2^10 if statement
C#
if(doctor_info.reg_num != ""){
q +="AND(educaation.reg_num = doctor_info.reg_num) ";
                 }
 
Share this answer
 
v2

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