Wednesday, December 7, 2022

Filtering in ASP.NET C# Select query in C# - Filtering in C#

 public DataTable ApplyFiltersWithSorting(DataTable dt, string SortOrder, string PresentDesignation)

        {

            string FMSID = txtSearchFMS.Text.ToString().PadLeft(6, '0');

            string Expression = "Name  like '%%' ", Exp = "";

            if (txtSearchFMS.Text.Length > 0)

                Exp += "and FMSID like '%" + FMSID + "%'";

            if (ddlInstitute.SelectedIndex > 0)

                Exp += "and InstituteId='" + ddlInstitute.SelectedValue + "'";

            if (ddlDisciplines.SelectedIndex > 0)

                Exp += "and Discipline='" + ddlDisciplines.SelectedItem.Text + "'";

            if (ddlAdjustedDiscipline.SelectedIndex > 0)

                Exp += "and AdjustedDiscipline='" + ddlAdjustedDiscipline.SelectedItem.Text + "'";

            if (ddlPresentDesignation.SelectedIndex > 0)

                Exp += "and PresentDesignation='" + ddlPresentDesignation.SelectedItem.Text + "'";

            else if (PresentDesignation.Length > 0)

                Exp += "and PresentDesignation like '" + PresentDesignation + "%'";

            if (ddlRosterCategory.SelectedIndex > 0)

                Exp += "and RosterCategory='" + ddlRosterCategory.SelectedItem.Text + "'";

            if (ddlSmd.SelectedIndex > 0)

                Exp += "and SMDID='" + ddlSmd.SelectedValue + "'";

            if (ddlWSP.SelectedIndex > 0)

                Exp += "and WSP='" + ddlWSP.SelectedValue.ToString() + "'";

            if (txtSearchByName.Text.Length > 0)

                Expression = "Name like '%" + txtSearchByName.Text + "%' " + Exp;

            if (ddlAIPR.SelectedIndex > 0)

            {

                if (ddlAIPR.SelectedValue.ToString() == "True")

                    Exp += "and Len(trim(AiprFile))>'" + 2 + "'";

                else

                    Exp += "and AiprFile is NULL";

            }

            if (ddlVigilance.SelectedIndex > 0)

            {

                if (ddlVigilance.SelectedValue == "4")

                    Exp += "and VigilanceStatusId is null";

                else

                    Exp += "and VigilanceStatusId='" + ddlVigilance.SelectedValue.ToString() + "'";

            }

            Expression += Exp;

            DataRow[] rows = dt.Select(Expression, SortOrder);

            DataTable dtnew = new DataTable();

            try

            {

                return rows.CopyToDataTable<DataRow>();

            }

            catch { return null; }

        }


//calling of above function

DataTable dt2 = ApplyFiltersWithSorting(dt, sort, "");

No comments:

Post a Comment