// OPERATION 1 == NUEVO - GAURDAR
 // OPERAATION 2 == ACTUALIZAR
 public PatientForm(int op, PatientModel p)
 {
     InitializeComponent();
     this.operation = op;
     this.patient   = p;
     llenarCmb();
     if (operation == 2)
     {
         txtName.Text              = patient.Name;
         txtApp.Text               = patient.Last_name;
         txtGenero.SelectedItem    = patient.Gender;
         txtDni.Text               = patient.Dni;
         txtGrado.Text             = patient.Grade;
         txtSitAdmin.Text          = patient.Sit_admin;
         txtStatePml.Text          = patient.State_pml;
         txtArma.Text              = patient.Arma;
         txtGuarnicion.Text        = patient.Guarnicion;
         cmbCategoria.SelectedItem = patient.CatetegoryValue;
     }
 }
        public bool delUser(PatientModel patient)
        {
            bool success = false;

            try
            {
                using (SqlConnection connection = new SqlConnection(DBConnection.stringConnection))
                {
                    SqlCommand command = new SqlCommand(null, connection);
                    command.Connection.Open();
                    command.CommandText = "DELETE   FROM  patients  WHERE patient_id =" + patient.Id;
                    command.Prepare();
                    command.ExecuteNonQuery();
                    success = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(success);
        }
        public Boolean createUser(PatientModel patient)
        {
            bool register = false;

            try
            {
                using (SqlConnection connection = new SqlConnection(DBConnection.stringConnection)) {
                    SqlCommand command = new SqlCommand(null, connection);
                    command.Connection.Open();
                    String sql = "INSERT INTO patients VALUES ('" + patient.Name + "','" + patient.Last_name + "'," + patient.GenderId + ",'" + patient.Dni + "','" + patient.Grade + "','" + patient.Sit_admin + "','" + patient.State_pml + "','" + patient.Arma + "','" + patient.Guarnicion + "'," + patient.Category + ", '" + patient.Created + "'" + ", '" + patient.Unit + "'" + ")";
                    Console.WriteLine(sql);
                    command.CommandText = sql;
                    command.Prepare();
                    command.ExecuteNonQuery();
                    register = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(register);
        }
        public FichaMedica(PatientModel p, User c)
        {
            InitializeComponent();
            this.currentUser           = c;
            this.patient               = p;
            this.catalogControllers    = new cmbController();
            this.appController         = new AppointmentController();
            this.appointmentsOfPatient = this.appController.getAppointmentsByPatientId(this.patient.Id, this.currentUser.Id);
            txtId.Text           = "000" + p.Id + "";
            txtNameApp.Text      = p.Name + " " + p.Last_name;
            txtGrade.Text        = p.Grade;
            txtUnidad.Text       = p.Unit;
            txtAdmin.Text        = p.Sit_admin;
            txtSexo.Text         = p.GenderId == 0 ? "Masculino" : "Femenino";
            txtDni.Text          = p.Dni;
            txtSexo.Text         = p.Gender;
            txtArma.Text         = p.Arma;
            txtCategoria.Text    = catalogControllers.findCategoryByName(p.CatetegoryValue).Name;
            txtSpecialty.Text    = catalogControllers.findSpecialtyById(currentUser.Specialty).Name;
            txtGuarnicion.Text   = p.Guarnicion;
            txtActasMédicas.Text = "0-" + this.appointmentsOfPatient.Rows.Count + "";

            txtDactasMedicas.Text    = "0-" + this.appointmentsOfPatient.Rows.Count + "";
            txtDNumEspecialidad.Text = currentUser.Specialty + "";
            txtDEspeciliad.Text      = txtSpecialty.Text;
            txtDNAditivo.Text        = txtId.Text;
            txtDNombrePaciente.Text  = txtNameApp.Text;
            txtDGrado.Text           = txtGrade.Text;
            txtDUnidad.Text          = txtUnidad.Text;
            txtDSitAdmin.Text        = txtAdmin.Text;
            t.Text                     = txtSexo.Text;
            txtDDNI.Text               = txtDni.Text;
            txtDArma.Text              = txtArma.Text;
            txtDGuarnicion.Text        = txtGuarnicion.Text;
            txtDCategoria.Text         = txtCategoria.Text;
            txtDDescanso.SelectedIndex = 0;
        }
        public bool editUser(PatientModel patient)
        {
            bool success = false;

            try
            {
                using (SqlConnection connection = new SqlConnection(DBConnection.stringConnection))
                {
                    SqlCommand command = new SqlCommand(null, connection);
                    command.Connection.Open();
                    String sql = "UPDATE    patients SET patient_name ='" + patient.Name + "', last_name = '" + patient.Last_name + "', gender =" + patient.GenderId + ",dni= '" + patient.Dni + "' , grade ='" + patient.Grade + "', sit_admin ='" + patient.Sit_admin + "', state_pml ='" + patient.State_pml + "', arma ='" + patient.Arma + "', guarnicion='" + patient.Guarnicion + "', category_id =" + patient.Category + ", unit='" + patient.Unit + "'" + " WHERE patient_id = " + patient.Id + ";";
                    Console.WriteLine(sql);
                    command.CommandText = sql;
                    command.Prepare();
                    command.ExecuteNonQuery();
                    success = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(success);
        }
 private void button5_Click_1(object sender, EventArgs e)
 {
     if (pacienteT.SelectedRows.Count > 0)
     {
         PatientModel p = new PatientModel();
         p.Id = int.Parse(pacienteT.SelectedRows[0].Cells[0].Value.ToString());
         DialogResult dialogResult = MessageBox.Show("¿Seguro de eliminar el registro?", "Atención", MessageBoxButtons.YesNo);
         if (dialogResult == DialogResult.Yes)
         {
             if (patient.delUser(p))
             {
                 MessageBox.Show("Eliminado correctamente!");
             }
         }
         else if (dialogResult == DialogResult.No)
         {
         }
     }
     else
     {
         MessageBox.Show("Selecciona un paciente en la tabla");
     }
     dataLoader();
 }