protected void gvPatients_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                int Id = Convert.ToInt32(gvPatients.DataKeys[e.RowIndex].Value.ToString());

                Patient patient = db.Patients.Where(p => p.Id == Id).FirstOrDefault <Patient>();

                patient.FirstName       = DESEncryptionHelper.Encrypt((gvPatients.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox).Text.Trim());
                patient.LastName        = DESEncryptionHelper.Encrypt((gvPatients.Rows[e.RowIndex].FindControl("txtLastName") as TextBox).Text.Trim());
                patient.Phone           = DESEncryptionHelper.Encrypt((gvPatients.Rows[e.RowIndex].FindControl("txtPhone") as TextBox).Text.Trim());
                patient.Email           = DESEncryptionHelper.Encrypt((gvPatients.Rows[e.RowIndex].FindControl("txtEmail") as TextBox).Text.Trim());
                patient.Gender          = DESEncryptionHelper.Encrypt((gvPatients.Rows[e.RowIndex].FindControl("ddlGender") as DropDownList).SelectedItem.Value);
                patient.Notes           = DESEncryptionHelper.Encrypt((gvPatients.Rows[e.RowIndex].FindControl("txtNotes") as TextBox).Text.Trim());
                patient.LastUpdatedDate = DateTime.Now;

                db.SaveChanges();
                gvPatients.EditIndex      = -1;
                Session["SuccessMessage"] = "Patient Record updated";
                Session["ErrorMessage"]   = "";
            }
            catch (Exception ex)
            {
                Session["SuccessMessage"] = "";
                Session["ErrorMessage"]   = "Error Updating Patient Record: " + ex.Message;
                if (ex.InnerException != null)
                {
                    Session["ErrorMessage"] += " Inner exception: " + ex.InnerException;
                }
            }
            finally
            {
                Response.Redirect(Request.RawUrl);
            }
        }
        private List <Patient> DecryptPatientData(List <Patient> patients)
        {
            List <Patient> listDecrypted = new List <Patient>();

            foreach (Patient patient in patients)
            {
                Patient decryptedPatient = new Patient
                {
                    Id              = patient.Id,
                    FirstName       = DESEncryptionHelper.Decrypt(patient.FirstName),
                    LastName        = DESEncryptionHelper.Decrypt(patient.LastName),
                    Phone           = DESEncryptionHelper.Decrypt(patient.Phone),
                    Email           = DESEncryptionHelper.Decrypt(patient.Email),
                    Gender          = DESEncryptionHelper.Decrypt(patient.Gender),
                    Notes           = DESEncryptionHelper.Decrypt(patient.Notes),
                    CreatedDate     = patient.CreatedDate,
                    LastUpdatedDate = patient.LastUpdatedDate,
                    IsDeleted       = patient.IsDeleted
                };

                listDecrypted.Add(decryptedPatient);
            }

            return(listDecrypted);
        }
        protected void gvPatients_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Add"))
            {
                try
                {
                    Patient patient = new Patient
                    {
                        FirstName       = DESEncryptionHelper.Encrypt((gvPatients.FooterRow.FindControl("txtFirstNameFooter") as TextBox).Text.Trim()),
                        LastName        = DESEncryptionHelper.Encrypt((gvPatients.FooterRow.FindControl("txtLastNameFooter") as TextBox).Text.Trim()),
                        Phone           = DESEncryptionHelper.Encrypt((gvPatients.FooterRow.FindControl("txtPhoneFooter") as TextBox).Text.Trim()),
                        Email           = DESEncryptionHelper.Encrypt((gvPatients.FooterRow.FindControl("txtEmailFooter") as TextBox).Text.Trim()),
                        Gender          = DESEncryptionHelper.Encrypt((gvPatients.FooterRow.FindControl("ddlGenderFooter") as DropDownList).SelectedItem.Value),
                        Notes           = DESEncryptionHelper.Encrypt((gvPatients.FooterRow.FindControl("txtNotesFooter") as TextBox).Text),
                        CreatedDate     = DateTime.Now,
                        LastUpdatedDate = DateTime.Now,
                        IsDeleted       = false
                    };

                    db.Patients.Add(patient);
                    db.SaveChanges();
                    Session["SuccessMessage"] = "New Patient Record Added";
                    Session["ErrorMessage"]   = "";
                }
                catch (Exception ex)
                {
                    Session["SuccessMessage"] = "";
                    Session["ErrorMessage"]   = "Error Adding Patient Record: " + ex.Message;
                    if (ex.InnerException != null)
                    {
                        Session["ErrorMessage"] += " Inner exception: " + ex.InnerException;
                    }
                }
                finally
                {
                    Response.Redirect(Request.RawUrl);
                }
            }
        }