Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I have this interface Dropbox - PUBLIC PHOTOS FOR FORUMS[^]

I created this in order for our HR Staff to filter employees and generate report according to their requirement (e.g All Faculty in Middle School, all american teachers, etc). I used to call this Flexible Employee Filtering

However, Do you have any suggestion on how to make this work without any convenience especially when using OR in WHERE clause? This is one of my dilemma

This is my code so far

C#
public void downloadOutput()
   {
       List<OrderFields> listOrderBy = OrderBy(); //PUTTING ALL FIELDS IN ORDER DEPENDING ON THE USER'S SELECTION IN ORDER FIELD TO BE USED IN 'ORDER BY'
       List<string> listFields = constructFieldNames(); //TO BE USED IN 'SELECT' ,FIELDS TO BE SHOWN,values from FIELDS interface
       List<string> listCondition = constructCondition(); //TO BE USED IN 'WHERE' CLAUSE, values from PARAMETERS INTERFACE
       string strFieldsToShow = string.Join(",", listFields);
       string strCondition = string.Join(" ", listCondition);
       string strOrderByFields = "";
       var newOrderByFields = new List<OrderFields>();
       newOrderByFields = listOrderBy;
       for (int i = 0; i <= listOrderBy.Count - 1; i++)
       {
           strOrderByFields += newOrderByFields[i].OrderField.ToString()+",";
       }
       strOrderByFields = strOrderByFields.Trim(',');
       string finalquery = "Select "+strFieldsToShow+" FROM "+
                       "EmploymentInfo LEFT JOIN EmpPersonalInfo "+
                       "ON EmploymentInfo.EmployeeNo=EmpPersonalInfo.EmployeeNo "+
                       "LEFT JOIN tblEmployeeMainInfo ON EmpPersonalInfo.EmployeeNo = tblEmployeeMainInfo.EmployeeCode "+
                       "LEFT JOIN EmploymentHistory ON EmpPersonalInfo.EmployeeNo = EmploymentHistory.EmployeeNo "+
                       "LEFT JOIN viewEmployeeApprover ON EmpPersonalInfo.EmployeeNo = viewEmployeeApprover.EmployeeNo "+
                       "LEFT JOIN EmployeePayType ON EmpPersonalInfo.EmployeeNo = EmployeePayType.EmployeeCode";

       if (strCondition.Trim() != "")
       {
           finalquery += " where " + strCondition;
       }

       if (strOrderByFields.Trim() != "")
       {
           finalquery += " order by " + strOrderByFields;
       }
       using (SqlConnection con = new SqlConnection(DAO.ConnectionString))
       {
           con.Open();
           using (SqlCommand cmd = new SqlCommand(finalquery, con))
           {
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataTable dt = new DataTable();
               da.Fill(dt);
               exportToExcel(dt);
           }
       }
   }



And these are the output of the strings.

strFieldsToShow = "EmpPersonalInfo.EmployeeNo, EmpPersonalInfo.LastName, EmpPersonalInfo.FirstName, tblEmployeeMainInfo.BranchCode"

strCondition = "EmploymentHistory.Status='TRUE' AND tblEmployeeMainInfo.LevelCode like 'FACULTY%'"

strOrderByFields="EmpPersonalInfo.Lastname,EmpPersonalInfo.FirstName,EmpPersonalInfo.BranchCode,EmpPersonalInfo.EmployeeNo"

Please note that TABLE NAMES are values of dropdownlist which I created in other class.

What I have tried:

I have tried the codes above, it is working except for when user selects OR for the where clause.
Posted
Updated 16-Feb-16 14:42pm
v2
Comments
an0ther1 16-Feb-16 20:35pm    
You should be using parameters in your query to avoid SQL Injection attacks but..
Could you please update your question to show what the strCondition string looks like.
Kind Regards
bjay tiamsic 16-Feb-16 20:45pm    
It did not work when I used parameters in query. I get an error "An expression of non-boolean type specified in a context where a condition is expected, near 'order'". I updated my question by the way
an0ther1 17-Feb-16 0:00am    
Take a look at this MSDN URL regarding how to avoid SQL Injection as an aside - https://technet.microsoft.com/en-us/library/ms161953(v=sql.105).aspx

Can you show me what finalQuery looks like when User selects Or for the where clause, run the code in a debugger & copy and paste the value to either a comment or to the original question
Ta
bjay tiamsic 17-Feb-16 0:29am    
Heres the finalQuery

Select EmpPersonalInfo.EmployeeNo,EmpPersonalInfo.LastName,EmpPersonalInfo.FirstName,tblEmployeeMainInfo.BranchCode FROM EmploymentInfo LEFT JOIN EmpPersonalInfo ON EmploymentInfo.EmployeeNo=EmpPersonalInfo.EmployeeNo LEFT JOIN tblEmployeeMainInfo ON EmpPersonalInfo.EmployeeNo = tblEmployeeMainInfo.EmployeeCode LEFT JOIN EmploymentHistory ON EmpPersonalInfo.EmployeeNo = EmploymentHistory.EmployeeNo LEFT JOIN viewEmployeeApprover ON EmpPersonalInfo.EmployeeNo = viewEmployeeApprover.EmployeeNo LEFT JOIN EmployeePayType ON EmpPersonalInfo.EmployeeNo = EmployeePayType.EmployeeCode where EmploymentHistory.Status = 'TRUE' AND tblEmployeeMainInfo.LevelCode LIKE 'FACULTY%' order by EmpPersonalInfo.LastName,EmpPersonalInfo.FirstName,tblEmployeeMainInfo.BranchCode,EmpPersonalInfo.EmployeeNo
an0ther1 17-Feb-16 15:27pm    
If the only difference is substituting 'AND' for 'OR' then the query should work as expected.
What happens? Do you get an error message or inaccurate results?

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