private void cmbDepartamentos_SelectedIndexChanged(object sender, EventArgs e)
        {
            string Dep   = cmbDepartamentos.SelectedItem.ToString();
            int    DepID = AdmDep.GetID(Dep);

            lblEnc.Text   = AdmDep.EncDep(DepID);
            lblDepto.Text = cmbDepartamentos.SelectedItem.ToString();
        }
        private void btnAgregar_Click(object sender, EventArgs e)
        {
            DialogResult Result = MessageBox.Show("¿DESEA AGREGAR ESTE EMPLEADO?", "PREGUNTA", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (Result == DialogResult.No)
            {
                return;
            }

            if (Utileria.IsEmpty(txtNombre.Text))
            {
                MessageBox.Show("NO SE ACEPTAN CAMPOS VACIOS EN EL NOMBRE", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (Utileria.IsEmpty(txtEmail.Text))
            {
                MessageBox.Show("NO SE ACEPTAN CAMPOS VACIOS EN EL EMAIL", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (Utileria.IsEmpty(txtDireccion.Text))
            {
                MessageBox.Show("NO SE ACEPTAN CAMPOS VACIOS EN LA DIRECCION", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (Utileria.ValidaTextoNum(txtCelular.Text))
            {
                MessageBox.Show("EN ESTE CAMPO SOLO SE ACEPTAN NUMEROS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int Rol = AdmEmp.GetIDByNameRol(cmbRol.SelectedItem.ToString());

            if (AdmEmp.AddEmp(txtNombre.Text, txtEmail.Text, txtCelular.Text, txtDireccion.Text, Rol))
            {
                MessageBox.Show("EMPLEADO AGREGADO EXITOSAMENRE", "INFORMACION", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            int DepID = AdmDepto.GetID(cmbDepto.SelectedItem.ToString());
            int EmpID = AdmEmp.GetIDByName(txtNombre.Text);

            SqlConnection Connection = UsoBD.ConectaBD(Utileria.GetConnectionString());

            if (Connection == null)
            {
                MessageBox.Show("ERROR DE CONEXIÓN CON LA BASE DE DATOS");

                foreach (SqlError E in UsoBD.ESalida.Errors)
                {
                    MessageBox.Show(E.Message);
                }
                return;
            }

            string strComando = "INSERT INTO EMPLEADO_DEPTO(ID_Empleado,ID_Depto)";

            strComando += " VALUES(@ID_Empleado, @ID_Depto)";

            SqlCommand Insert = new SqlCommand(strComando, Connection);

            Insert.Parameters.AddWithValue("@ID_Empleado", EmpID);
            Insert.Parameters.AddWithValue("@ID_Depto", DepID);

            try
            {
                Insert.ExecuteNonQuery();
            }
            catch (SqlException Ex)
            {
                foreach (SqlError item in Ex.Errors)
                {
                    MessageBox.Show(item.Message);
                }

                Connection.Close();
            }

            Connection.Close();
        }
Exemplo n.º 3
0
        private void btnActualizar_Click(object sender, EventArgs e)
        {
            DialogResult Result = MessageBox.Show("¿DESEA GUARDAR LOS CAMBIOS?", "PREGUNTA", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (Result == DialogResult.No)
            {
                return;
            }

            int    ID     = AdmEmp.GetIDName(cmbEncargado.SelectedItem.ToString());
            string Nombre = txtNombre.Text;
            string NumCel = txtNumCel.Text;
            string Email  = txtEmail.Text;
            string Dir    = txtDir.Text;


            if (!(Utileria.IsEmpty(Nombre)))
            {
                AdmEmp.UpdateNom(ID, Nombre);
            }
            if (!(Utileria.IsEmpty(NumCel)))
            {
                AdmEmp.UpdateCel(ID, NumCel);
            }
            if (!(Utileria.IsEmpty(Email)))
            {
                AdmEmp.UpdateEmail(ID, Email);
            }
            if (!(Utileria.IsEmpty(Dir)))
            {
                AdmEmp.UpdateDir(ID, Dir);
            }
            if (cmbRol.SelectedIndex >= 0)
            {
                SqlConnection Connection = UsoBD.ConectaBD(Utileria.GetConnectionString());
                int           Rol        = 0;

                if (Connection == null)
                {
                    MessageBox.Show("ERROR DE CONEXIÓN CON LA BASE DE DATOS");

                    foreach (SqlError E in UsoBD.ESalida.Errors)
                    {
                        MessageBox.Show(E.Message);
                    }
                    return;
                }
                SqlDataReader Lector = null;

                string strComandoC = "SELECT ID FROM ROL WHERE NOMBRE LIKE '" + cmbRol.SelectedItem.ToString() + "'";

                Lector = UsoBD.Consulta(strComandoC, Connection);
                if (Lector == null)
                {
                    MessageBox.Show("ERROR AL HACER LA CONSULTA");
                    foreach (SqlError E in UsoBD.ESalida.Errors)
                    {
                        MessageBox.Show(E.Message);
                    }

                    Connection.Close();
                    return;
                }
                if (Lector.HasRows)
                {
                    while (Lector.Read())
                    {
                        Rol = Convert.ToInt32(Lector.GetValue(0).ToString());
                    }
                }
                Connection.Close();

                AdmEmp.UpdateRolID(ID, Rol);
            }
            if (cmbDep.SelectedIndex >= 0)
            {
                int DepID = AdmDept.GetID(cmbDep.SelectedItem.ToString());
                AdmDept.UpdateDep(ID, DepID);
            }
            MessageBox.Show("EMPLEADO ACTUALIZADO EXITOSAMENTE", "AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (lvDepartamentos.SelectedItems.Count == 0)
            {
                MessageBox.Show("NO HA SELECCIONADO NINGUN DEPARTAMENTO", "SIN SELECCIONAR", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult R = MessageBox.Show("¿DESEA ELIMINAR EL DEPARTAMENTO SELECCIONADO?", "CONFIRMAR", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (R == DialogResult.Yes)
            {
                string NA;
                for (int i = 0; i < lvDepartamentos.Items.Count; i++)
                {
                    if (lvDepartamentos.SelectedItems.Contains(lvDepartamentos.Items[i]))
                    {
                        NA = lvDepartamentos.Items[i].SubItems[0].Text;
                        int DepID = AdmDept.GetID(NA);
                        if (true)
                        {
                            SqlConnection Connection3  = UsoBD.ConectaBD(Utileria.GetConnectionString());
                            SqlDataReader Lector3      = null;
                            string        strComandoC3 = "SELECT ID_Empleado FROM EMPLEADO_DEPTO WHERE ID_Depto=" + DepID;

                            Lector3 = UsoBD.Consulta(strComandoC3, Connection3);
                            if (Lector3 == null)
                            {
                                MessageBox.Show("ERROR AL HACER LA CONSULTA");
                                foreach (SqlError E in UsoBD.ESalida.Errors)
                                {
                                    MessageBox.Show(E.Message);
                                }

                                Connection3.Close();
                                return;
                            }
                            if (Lector3.HasRows)
                            {
                                while (Lector3.Read())
                                {
                                    AdmDept.UpdateEstatus(DepID);
                                    MessageBox.Show("LA BAJA DEL DEPARTAMENTO SE HA REALIZADO CORRECTAMENTE", "ELIMINACION REALIZADA", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    lvDepartamentos.Items[i].Remove();
                                    Connection3.Close();
                                    return;
                                }
                            }
                            Connection3.Close();
                        }

                        SqlConnection Connection = UsoBD.ConectaBD(Utileria.GetConnectionString());

                        if (Connection == null)
                        {
                            MessageBox.Show("ERROR DE CONEXIÓN CON LA BASE DE DATOS");

                            foreach (SqlError E in UsoBD.ESalida.Errors)
                            {
                                MessageBox.Show(E.Message);
                            }

                            return;
                        }

                        SqlDataReader Lector = null;

                        string strComandoC = "DELETE FROM DEPARTAMENTO WHERE Nombre LIKE '" + NA + "'";

                        Lector = UsoBD.Consulta(strComandoC, Connection);
                        if (Lector == null)
                        {
                            MessageBox.Show("ERROR AL HACER LA CONSULTA");
                            foreach (SqlError E in UsoBD.ESalida.Errors)
                            {
                                MessageBox.Show(E.Message);
                            }

                            Connection.Close();
                            return;
                        }
                        Connection.Close();
                        lvDepartamentos.Items[i].Remove();
                        MessageBox.Show("LA ELIMINACION DEL DEPARTAMENTO SE HA REALIZADO CORRECTAMENTE", "ELIMINACION REALIZADA", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            }
        }