protected void ButtonDownload_Click(object sender, EventArgs e)
        {
            try
            {
                // Retrieve gen settings
                GeneralizedSetting generalizedSetting = dataBLL.GetGeneralizedSettingFromDb();

                #region Validation: Match and Get only valid inputs
                FilteredValues filteredValues = new FilteredValues();

                // Marital Status
                foreach (ListItem item in inputMaritalStatusLevel.Items)
                {
                    if (item.Selected)
                    {
                        if (generalizedSetting.maritalStatusOptions.Any(t => t.Item1.Equals(item.Value.Trim())))
                        {
                            filteredValues.maritalStatus.Add(item.Value.Trim());
                        }
                    }
                }

                // Sex
                foreach (ListItem item in inputSexLevel.Items)
                {
                    if (item.Selected)
                    {
                        if (generalizedSetting.sexOptions.Any(t => t.Item1.Equals(item.Value.Trim())))
                        {
                            filteredValues.sex.Add(item.Value.Trim());
                        }
                    }
                }

                // Gender
                foreach (ListItem item in inputGenderLevel.Items)
                {
                    if (item.Selected)
                    {
                        if (generalizedSetting.genderOptions.Any(t => t.Item1.Equals(item.Value.Trim())))
                        {
                            filteredValues.gender.Add(item.Value.Trim());
                        }
                    }
                }

                // Age
                foreach (ListItem item in inputAgeLevel.Items)
                {
                    if (item.Selected)
                    {
                        if (generalizedSetting.ageOptions.Any(t => t.Item1.Equals(item.Value.Trim())))
                        {
                            filteredValues.age.Add(item.Value.Trim());
                        }
                    }
                }

                // Postal
                DataTable postalTable = dataBLL.GetPostal();
                foreach (ListItem item in inputPostal.Items)
                {
                    if (item.Selected)
                    {
                        if (postalTable.AsEnumerable().Any(row => row.Field <string>("postal").Equals(item.Value.Trim())))
                        {
                            filteredValues.postal.Add(item.Value.Trim());
                        }
                    }
                }

                // Diagnosis
                DataTable diagnosesTable = dataBLL.GetDiagnoses();
                foreach (ListItem item in inputDiagnosis.Items)
                {
                    if (item.Selected)
                    {
                        if (diagnosesTable.AsEnumerable().Any(row => row.Field <string>("diagnosis_code").Equals(item.Value.Trim())))
                        {
                            filteredValues.diagnoses.Add(item.Value.Trim());
                        }
                    }
                }


                // Record Type
                foreach (ListItem item in inputRecordType.Items)
                {
                    if (item.Selected)
                    {
                        filteredValues.recordType.Add(item.Value.Trim());
                    }
                }

                // Record Diagnosis
                DataTable recordDiagnosesTable = dataBLL.GetRecordDiagnoses();
                foreach (ListItem item in inputRecordDiagnosis.Items)
                {
                    if (item.Selected)
                    {
                        if (recordDiagnosesTable.AsEnumerable().Any(row => row.Field <string>("diagnosis_code").Equals(item.Value.Trim())))
                        {
                            filteredValues.recordDiagnoses.Add(item.Value.Trim());
                        }
                    }
                }

                #endregion

                DataTable anonPatientsTable = dataBLL.GetPatientsForDownload(filteredValues);

                string delimiter = ",";

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (StreamWriter streamWriter = new StreamWriter(memoryStream))
                    {
                        for (int i = 0; i < anonPatientsTable.Columns.Count; i++)
                        {
                            streamWriter.Write(anonPatientsTable.Columns[i].ColumnName);
                            streamWriter.Write((i < anonPatientsTable.Columns.Count - 1) ? delimiter : Environment.NewLine);
                        }

                        foreach (DataRow row in anonPatientsTable.Rows)
                        {
                            for (int i = 0; i < anonPatientsTable.Columns.Count; i++)
                            {
                                if (row[i].ToString().IndexOf(",") > -1)
                                {
                                    streamWriter.Write("\"" + row[i].ToString() + "\"");
                                }
                                else
                                {
                                    streamWriter.Write(row[i].ToString());
                                }
                                streamWriter.Write((i < anonPatientsTable.Columns.Count - 1) ? delimiter : Environment.NewLine);
                            }
                        }
                    }

                    Response.Clear();
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.ContentType = "text/csv";
                    Response.Charset     = string.Empty;
                    Response.Cache.SetCacheability(HttpCacheability.Public);
                    Response.AddHeader("Content-Disposition", "attachment; filename=\"anon_data.csv\"");
                    Response.BinaryWrite(memoryStream.ToArray());
                }

                ViewState["GridViewPatientAnonymised"] = null;
                GridViewPatientAnonymised.DataSource   = null;
                GridViewPatientAnonymised.DataBind();
                PanelViewHeader.Visible = false;
                PanelView.Visible       = false;
                UpdatePanelPatientAnonymised.Update();
                Response.Flush();
                Response.Close();
            }
            catch
            {
            }
        }
        protected void ButtonFilter_ServerClick(object sender, EventArgs e)
        {
            try
            {
                // Retrieve gen settings
                GeneralizedSetting generalizedSetting = dataBLL.GetGeneralizedSettingFromDb();

                #region Validation: Match and Get only valid inputs
                FilteredValues filteredValues = new FilteredValues();

                // Marital Status
                foreach (ListItem item in inputMaritalStatusLevel.Items)
                {
                    if (item.Selected)
                    {
                        if (generalizedSetting.maritalStatusOptions.Any(t => t.Item1.Equals(item.Value.Trim())))
                        {
                            filteredValues.maritalStatus.Add(item.Value.Trim());
                        }
                    }
                }

                // Sex
                foreach (ListItem item in inputSexLevel.Items)
                {
                    if (item.Selected)
                    {
                        if (generalizedSetting.sexOptions.Any(t => t.Item1.Equals(item.Value.Trim())))
                        {
                            filteredValues.sex.Add(item.Value.Trim());
                        }
                    }
                }

                // Gender
                foreach (ListItem item in inputGenderLevel.Items)
                {
                    if (item.Selected)
                    {
                        if (generalizedSetting.genderOptions.Any(t => t.Item1.Equals(item.Value.Trim())))
                        {
                            filteredValues.gender.Add(item.Value.Trim());
                        }
                    }
                }

                // Age
                foreach (ListItem item in inputAgeLevel.Items)
                {
                    if (item.Selected)
                    {
                        if (generalizedSetting.ageOptions.Any(t => t.Item1.Equals(item.Value.Trim())))
                        {
                            filteredValues.age.Add(item.Value.Trim());
                        }
                    }
                }

                // Postal
                DataTable postalTable = dataBLL.GetPostal();
                foreach (ListItem item in inputPostal.Items)
                {
                    if (item.Selected)
                    {
                        if (postalTable.AsEnumerable().Any(row => row.Field <string>("postal").Equals(item.Value.Trim())))
                        {
                            filteredValues.postal.Add(item.Value.Trim());
                        }
                    }
                }

                // Diagnosis
                DataTable diagnosesTable = dataBLL.GetDiagnoses();
                foreach (ListItem item in inputDiagnosis.Items)
                {
                    if (item.Selected)
                    {
                        if (diagnosesTable.AsEnumerable().Any(row => row.Field <string>("diagnosis_code").Equals(item.Value.Trim())))
                        {
                            filteredValues.diagnoses.Add(item.Value.Trim());
                        }
                    }
                }


                // Record Type
                foreach (ListItem item in inputRecordType.Items)
                {
                    if (item.Selected)
                    {
                        filteredValues.recordType.Add(item.Value.Trim());
                    }
                }

                // Record Diagnosis
                DataTable recordDiagnosesTable = dataBLL.GetRecordDiagnoses();
                foreach (ListItem item in inputRecordDiagnosis.Items)
                {
                    if (item.Selected)
                    {
                        if (recordDiagnosesTable.AsEnumerable().Any(row => row.Field <string>("diagnosis_code").Equals(item.Value.Trim())))
                        {
                            filteredValues.recordDiagnoses.Add(item.Value.Trim());
                        }
                    }
                }
                #endregion

                List <PatientAnonymised> recordAnonymised = dataBLL.GetPatients(filteredValues);
                ViewState["GridViewPatientAnonymised"] = recordAnonymised;
                GridViewPatientAnonymised.DataSource   = recordAnonymised;
                GridViewPatientAnonymised.DataBind();
                PanelViewHeader.Visible = true;
                PanelView.Visible       = true;
                UpdatePanelPatientAnonymised.Update();

                ScriptManager.RegisterStartupScript(this, GetType(), "alert", "toastr['success']('Data successfully displayed.');", true);
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "alert", "toastr['error']('Error occured when displaying data.');", true);
            }
        }