//metodo para eliminar ejercicios public static int EliminarProfe(Profesores ejer, String query) { int retorno = 0; MySqlCommand cmd = new MySqlCommand(String.Format(query, ejer.Dni), Conexion.obtenerConexion()); retorno = cmd.ExecuteNonQuery(); return(retorno); }
//metodo para modificar datos profesor public static int ModificarProfe(Profesores profe, String query) { int retorno = 0; string nombreApe = profe.Apellido + " " + profe.Nombre; MySqlCommand cmd = new MySqlCommand(String.Format(query, profe.Dni, nombreApe, profe.Celular, profe.CelEmergencia, profe.TipoSangre), Conexion.obtenerConexion()); retorno = cmd.ExecuteNonQuery(); return(retorno); }
//metodo para obtener un profesor public static Profesores ObtenerProfe(int dni) { Profesores ejer = new Profesores(); MySqlCommand cmd = new MySqlCommand(String.Format("select dniProfe, nombreApe, celular, celEmergencia, tipoSangre from profesores where dniProfe = '{0}'", dni), Conexion.obtenerConexion()); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { ejer.Apellido = reader.GetString(1); ejer.Celular = reader.GetDouble(2); ejer.CelEmergencia = reader.GetDouble(3); ejer.TipoSangre = reader.GetInt32(4); ejer.Dni = reader.GetInt32(0); } return(ejer); }
//BTN que borra el ejercicio seleccionado y actualiza la grilla. private void button1_Click(object sender, EventArgs e) { if (dgv_todosLosProfes.SelectedRows.Count == 1) { //obtiene el id del ejercicio que se selecciono int dni = Convert.ToInt32(dgv_todosLosProfes.CurrentRow.Cells["DNI"].Value); //busca el ejercicio con el id encontrado recien profeSeleccionado = Clases.Metodos.ObtenerProfe(dni); string query = "DELETE FROM profesores where dniProfe = '{0}'"; if (MessageBox.Show("Esta Seguro que desea eliminar el Profesor", "Advertencia", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { if (Clases.Metodos.EliminarProfe(profeSeleccionado, query) > 0) { //txt_nombreEjercicioNuevo.Clear(); //cmb_tipoEjercicioAgregar.SelectedItem = null; MessageBox.Show("Profesor Eliminado Correctamente!", "Ejercicio Eliminado", MessageBoxButtons.OK, MessageBoxIcon.Information); //actualizo el data grid view #region Clases.Metodos m = new Clases.Metodos(); string query2 = "SELECT profesores.nombreApe as 'Profesor', profesores.dniProfe as 'DNI', profesores.celular as 'Celular', " + "profesores.celEmergencia as 'Cel Emergencia', tiposangre.nombre as 'Grupo Sanguineo' " + "FROM profesores JOIN tiposangre ON profesores.tipoSangre = tiposangre.id ORDER BY 1"; //llena de grilla con todos los ejercicios m.LlenarGridProfesores(dgv_todosLosProfes, query2); #endregion } else { MessageBox.Show("No se pudo eliminar el Ejercicio", "Ejercicio No Eliminado", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else { MessageBox.Show("Se cancelo la eliminacion", "Eliminacion Cancelada", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }
//metodo para buscar un profesor public static List <Profesores> BuscarProfe(string nombre) { List <Profesores> _lista = new List <Profesores>(); MySqlCommand cmd = new MySqlCommand(String.Format("SELECT profesores.nombreApe as 'Profesor', profesores.dniProfe as 'DNI', " + "profesores.celular as 'Celular', profesores.celEmergencia as 'Cel Emergencia', tiposangre.nombre as 'Grupo Sanguíneo' " + "FROM profesores JOIN tiposangre ON profesores.tipoSangre = tiposangre.id " + "where profesores.nombreApe LIKE '%{0}%' ORDER BY 1 ", nombre), Conexion.obtenerConexion()); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Profesores ejer = new Profesores(); ejer.Nombre = reader.GetString(0); ejer.Apellido = reader.GetString(0); ejer.Dni = reader.GetInt32(1); ejer.Celular = reader.GetDouble(2); ejer.CelEmergencia = reader.GetDouble(3); _lista.Add(ejer); } return(_lista); }
//BUTTON que al presionarlo obtiene el profesor seleccionado y agrega en cada campo correspondiente para poder modificarlo private void button2_Click(object sender, EventArgs e) { if (dgv_todosLosProfes.SelectedRows.Count == 1) { //obtiene el DNI del Profe que se selecciono int dni = Convert.ToInt32(dgv_todosLosProfes.CurrentRow.Cells["DNI"].Value); //busca el ejercicio con el id encontrado recien profeSeleccionado = Clases.Metodos.ObtenerProfe(dni); } //si encontro el ejercicio, lo busca y lo muestra en los campos correspondientes if (profeSeleccionado != null) { txt_docProfe.Enabled = false; txt_nombreProfe.Visible = false; txt_nombreProfe.Text = ""; txt_apellidoProfe.Text = profeSeleccionado.Apellido; txt_apellidoProfe.ForeColor = Color.Black; txt_docProfe.Text = Convert.ToString(profeSeleccionado.Dni); if (txt_docProfe.Enabled == false) { errorProvider1.SetError(txt_docProfe, "No se puede modificar el DNI"); } else { errorProvider1.Clear(); } txt_celProfe.Text = profeSeleccionado.Celular.ToString(); txt_celProfe.ForeColor = Color.Black; txt_cerEmergenciaProfe.Text = Convert.ToString(profeSeleccionado.CelEmergencia); txt_cerEmergenciaProfe.ForeColor = Color.Black; btn_registrarProfe.Text = "GUARDAR"; gb_profesores.Visible = false; } //Metodos que controlan de que tipo es para cargar el comboBox #region if (dgv_todosLosProfes.SelectedRows.Count == 0) { MessageBox.Show("Debe seleccionar una fila", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (profeSeleccionado.TipoSangre == 1) { cmb_sangreProfe.SelectedIndex = 0; } if (profeSeleccionado.TipoSangre == 2) { cmb_sangreProfe.SelectedIndex = 1; } if (profeSeleccionado.TipoSangre == 3) { cmb_sangreProfe.SelectedIndex = 2; } if (profeSeleccionado.TipoSangre == 4) { cmb_sangreProfe.SelectedIndex = 3; } if (profeSeleccionado.TipoSangre == 5) { cmb_sangreProfe.SelectedIndex = 4; } if (profeSeleccionado.TipoSangre == 6) { cmb_sangreProfe.SelectedIndex = 5; } if (profeSeleccionado.TipoSangre == 7) { cmb_sangreProfe.SelectedIndex = 6; } if (profeSeleccionado.TipoSangre == 8) { cmb_sangreProfe.SelectedIndex = 7; } } #endregion txt_buscarPorNombre.Clear(); }
//metodo para REGISTRAR / MODIFICAR profesores private void txt_registrarProfe_Click(object sender, EventArgs e) { if (ValidarDatos()) { Clases.Profesores ejer = new Clases.Profesores(); string queryProfe = "SELECT COUNT(profesores.dniProfe) from profesores where profesores.dniProfe = " + Convert.ToInt32(txt_docProfe.Text); int validarProfe = Clases.Metodos.ValidarProfe(Convert.ToInt32(txt_docProfe.Text), queryProfe); //si el TXT_DOCUMENTO esta vacio lo agrega, de lo contrario lo modifica. if (validarProfe > 0) { //consulta para actualizar el ejercicio string query = "UPDATE profesores set nombreApe = '{1}', celular = '{2}', celEmergencia = '{3}', tipoSangre = '{4}' where dniProfe = '{0}'"; ejer.Apellido = txt_apellidoProfe.Text; String nomApe = ejer.Nombre + " " + ejer.Apellido; ejer.Celular = Convert.ToDouble(txt_celProfe.Text); ejer.CelEmergencia = Convert.ToDouble(txt_cerEmergenciaProfe.Text); ejer.TipoSangre = cmb_sangreProfe.SelectedIndex + 1; ejer.Dni = Convert.ToInt32(txt_docProfe.Text); //llamo al metodo de la clase "Metodos" y le paso por parametro el ejercicio creado y la consulta int retorno = Clases.Metodos.ModificarProfe(ejer, query); if (retorno > 0) { MessageBox.Show("Profesor modificado correctamente!", "Profesor Modificado", MessageBoxButtons.OK, MessageBoxIcon.Information); txt_nombreProfe.Visible = true; txt_nombreProfe.Text = "NOMBRE"; txt_apellidoProfe.Text = "APELLIDO"; txt_docProfe.Text = "NRO DOCUMENTO"; txt_celProfe.Text = "CELULAR"; txt_cerEmergenciaProfe.Text = "CEL EMERGENCIA"; cmb_sangreProfe.SelectedItem = null; btn_registrarProfe.Text = "REGISTRAR"; } else { MessageBox.Show("Ocurrió un error"); } } else { //consulta para guardar el profesor string query = "insert into profesores (dniProfe, nombreApe, celular, celEmergencia, tipoSangre) values ('{0}', '{1}', '{2}', '{3}', '{4}')"; ejer.Nombre = txt_nombreProfe.Text; ejer.Apellido = txt_apellidoProfe.Text; ejer.Dni = Convert.ToInt32(txt_docProfe.Text); ejer.Celular = Convert.ToDouble(txt_celProfe.Text); ejer.CelEmergencia = Convert.ToDouble(txt_cerEmergenciaProfe.Text); ejer.TipoSangre = cmb_sangreProfe.SelectedIndex + 1; //llamo al metodo de la clase "Metodos" y le paso por parametro el profesor creado y la consulta int retorno = Clases.Metodos.AgregarProfesor(ejer, query); if (retorno > 0) { MessageBox.Show("Se agrego correctamente!", "Profesor Agregado", MessageBoxButtons.OK, MessageBoxIcon.Information); txt_nombreProfe.Clear(); txt_apellidoProfe.Clear(); txt_docProfe.Clear(); txt_celProfe.Clear(); txt_cerEmergenciaProfe.Clear(); cmb_sangreProfe.SelectedItem = null; //actualizo el data grid view #region Clases.Metodos m = new Clases.Metodos(); string query2 = "SELECT profesores.nombreApe as 'Profesor', profesores.dniProfe as 'DNI', profesores.celular as 'Celular', " + "profesores.celEmergencia as 'Cel Emergencia', tiposangre.nombre as 'Grupo Sanguineo' " + "FROM profesores JOIN tiposangre ON profesores.tipoSangre = tiposangre.id ORDER BY 1"; //llena de grilla con todos los ejercicios m.LlenarGridEjercicios(dgv_todosLosProfes, query2); #endregion //cierro el FORM this.Close(); } else { MessageBox.Show("Ocurrió un error"); } } } }