Friday, January 28, 2011

Critical SQL statement

protected void BuildWhereClause()
        {
            string skillresult = "";
            string qualificationresult = "";
            string cityresult = "";
            string name="";
            string experincefrom = "";
            string experinceto = "";
            string fresher = "";
            string interships = "";
            string lacsfrom = "";
            string lacsto = "";
            string companyresult = "";
            string activedays = "";
            string skilljoin = " inner join tblCandidateSkillSets ss on cp.ID = ss.CandidateID inner join   tblSkillSetLookup lp on lp.ID = ss.SkillID";
            string skillwhere = " where lp.Skill in";
            string qualificationjoin = " inner join tblCandidateQualification qf on cp.ID=qf.CandidateID inner join tblQualificationLookup ql on ql.ID=qf.QualificationID";
            string qualificationwhere = " ql.Qualification in";
//skills loop
            int count = chkSkills.Items.Count;
            for (int i = 0; i < count; i++)
            {
                if (chkSkills.Items[i].Selected == true)
                {
                    skillresult += "," + "'" + (chkSkills.Items[i].Value) + "'";
                }
            }
            if (skillresult.Length > 0)
            {
                skillresult = skillresult.Substring(1);
                sqljoin = skilljoin + skillwhere + "(" + skillresult + ")";
            }

            //Qualification loop
            int z = chkQualification.Items.Count;
            for (int k = 0; k < z; k++)
            {
                if (chkQualification.Items[k].Selected == true)
                {
                    qualificationresult += "," + "'" + (chkQualification.Items[k].Value) + "'";
                }
            }
            if (qualificationresult.Length > 0)
            {
                qualificationresult = qualificationresult.Substring(1);
                if (sqljoin.Length > 0)
                {
                    sqljoin = skilljoin + qualificationjoin + skillwhere + "(" + skillresult + ")" + "and" + qualificationwhere + "(" + qualificationresult + ")";
                }
                else
                {
                    sqljoin = qualificationjoin + " where " + qualificationwhere + "(" + qualificationresult + ")";
                }
            }

            //location loop
            int y = chkCity.Items.Count;
            for (int j = 0; j < y; j++)
            {
                if (chkCity.Items[j].Selected == true)
                {
                    cityresult += "," + "'" + (chkCity.Items[j].Value + "'");
                }
            }
            if (cityresult.Length > 0)
            {
                cityresult = cityresult.Substring(1);
                if (sqljoin.Length > 0)
                {
                    sqljoin = sqljoin + " and CurrentLocation in(" + cityresult + ")";
                }
                else
                {
                    sqljoin = " where CurrentLocation in(" + cityresult + ")";
                }
            }
//If name is given to search name loop
            if(txtName.Text.Length>0)
            {                         
                name=txtName.Text;
                if (sqljoin.Length > 0)
                {
                    sqljoin = sqljoin + " and Name=" + "'" + name + "'";
                }
                else
                {
                    sqljoin = " where Name=" + "'" + name + "'";
                }
            }


            //Experince
            if ((ddlYearFrom.SelectedIndex > 0) && (ddlYearTo.SelectedIndex > 0))
            {
                experincefrom = ddlYearFrom.SelectedValue;
                experinceto = ddlYearTo.SelectedValue;
                if (sqljoin.Length > 0)
                {
                    sqljoin = sqljoin + " and ExpInYrs between " + experincefrom + " and " + experinceto;
                }
                else
                {
                    sqljoin = " where ExpInYrs between " + experincefrom + " and " + experinceto;
                }
            }
            else
            {
                if ((ddlYearFrom.SelectedIndex > 0) || (ddlYearTo.SelectedIndex > 0))
                {
                    experincefrom = ddlYearFrom.SelectedValue;
                    experinceto = ddlYearTo.SelectedValue;
                    if (skillresult.Length > 0)
                    {
                        if (ddlYearFrom.SelectedIndex > 0)
                        {
                            sqljoin = sqljoin + " and ExpInYrs=" + experincefrom;
                        }
                        else
                        {
                            sqljoin = sqljoin + " and ExpInYrs=" + experinceto;
                        }
                    }
                    else
                    {
                        if (ddlYearTo.SelectedIndex > 0)
                        {
                            sqljoin = sqljoin + " where ExpInYrs=" + experinceto;
                        }
                        else
                        {
                            sqljoin = sqljoin + " where ExpInYrs=" + experincefrom;
                        }
                    }
                }
            }

            //company
            if (txtCompany.Text.Length > 0)
            {
                companyresult = txtCompany.Text;
                if (sqljoin.Length > 0)
                {
                    sqljoin = sqljoin + " and CurrentCompany=" + "'" + companyresult + "'";
                }
                else
                {
                    sqljoin = " where CurrentCompany=" + "'" + companyresult + "'";
                }
            }

            //If Fresher
            if (chkFreshers.Checked == true)
            {
                fresher = "1";
                if (sqljoin.Length > 0)
                {
                    sqljoin = sqljoin + " and Fresher =" + fresher;
                }
                else
                {
                    sqljoin = " where Fresher =" + fresher;
                }
            }
//If internship
            if (chkInternships.Checked == true)
            {
                interships = "1";
                if (sqljoin.Length > 0)
                {
                    sqljoin = sqljoin + " and Internship =" + interships;
                }
                else
                {
                    sqljoin = " where Internship =" + interships;
                }
            }

            //salary in lacs
            if ((ddlLacsFrom.SelectedIndex > 0) && (ddlLacsTo.SelectedIndex > 0))
            {
                lacsfrom = ddlLacsFrom.SelectedValue;
                lacsto = ddlLacsTo.SelectedValue;
                if (sqljoin.Length > 0)
                {
                    sqljoin = sqljoin + " and CurrentSalaryInLacs between " + lacsfrom + " and " + lacsto;
                }
                else
                {
                    sqljoin = " where CurrentSalaryInLacs between " + lacsfrom + " and " + lacsto;
                }
            }
            else
            {
                if ((ddlLacsFrom.SelectedIndex > 0) || (ddlLacsTo.SelectedIndex > 0))
                {
                    lacsfrom = ddlLacsFrom.SelectedValue;
                    lacsto = ddlLacsTo.SelectedValue;
                    if (sqljoin.Length > 0)
                    {
                        if (ddlLacsFrom.SelectedIndex > 0)
                        {
                            sqljoin = sqljoin + " and CurrentSalaryInLacs=" + lacsfrom;
                        }
                        else
                        {
                            sqljoin = sqljoin + " and CurrentSalaryInLacs=" + lacsto;
                        }
                    }
                    else
                    {
                        if (ddlLacsTo.SelectedIndex > 0)
                        {
                            sqljoin = sqljoin + " where CurrentSalaryInLacs=" + lacsto;
                        }
                        else
                        {
                            sqljoin = sqljoin + " where CurrentSalaryInLacs=" + lacsfrom;
                        }
                    }
                }
            }

            // If company name is given to search company
            if (txtCompany.Text.Length > 0)
            {
                companyresult = txtCompany.Text;
                if (sqljoin.Length > 0)
                {
                    sqljoin = sqljoin + " and CurrentCompany=" + "'" + companyresult + "'";
                }
                else
                {
                    sqljoin = " where CurrentCompany=" + "'" + companyresult + "'";
                }
            }

            //If active days is selected
            if (ddlActive.SelectedIndex > 0)
            {
                activedays = ddlActive.SelectedValue;
                if (ddlActive.SelectedValue == "1 Week")
                {
                    if (sqljoin.Length > 0)
                    {
                        sqljoin = sqljoin + " and UpdatedOn<=dateadd(day," + 7 + ",getdate())";
                    }
                    else
                    {
                        sqljoin = sqljoin + " where UpdatedOn<=dateadd(day," + 7 + ",getdate())";
                    }
                }
                if (ddlActive.SelectedValue == "1 Month")
                {
                    if (sqljoin.Length > 0)
                    {
                        sqljoin = sqljoin + " and UpdatedOn<=dateadd(day," + 30 + ",getdate())";
                    }
                    else
                    {
                        sqljoin = sqljoin + " where UpdatedOn<=dateadd(day," + 30 + ",getdate())";
                    }
                }
                if (ddlActive.SelectedValue == "3 Months")
                {
                    if (sqljoin.Length > 0)
                    {
                        sqljoin = sqljoin + " and UpdatedOn<=dateadd(day," + 90 + ",getdate())";
                    }
                    else
                    {
                        sqljoin = sqljoin + " where UpdatedOn<=dateadd(day," + 90 + ",getdate())";
                    }
                }
                if (ddlActive.SelectedValue == "6 Months")
                {
                    if (sqljoin.Length > 0)
                    {
                        sqljoin = sqljoin + " and UpdatedOn<=dateadd(day," + 180 + ",getdate())";
                    }
                    else
                    {
                        sqljoin = sqljoin + " where UpdatedOn<=dateadd(day," + 180 + ",getdate())";
                    }
                }

            }

            //To exclude already viewed profiles
            if (chkExclude.Checked == true)
            {
                GetUserName();
                if (sqljoin.Length > 0)
                {
                    sqljoin = sqljoin + " and ViewedUsers like'; " + users + ";'";
                }
                else
                {
                    sqljoin = " where ViewedUsers like'; " + users + ";'";
                }
            }
        }

No comments:

Post a Comment