// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ALMACENAR DATOS ENTIDAD private void almacenarDatosEntidad() { DSKTUSERS entidad = asignDataFormToEntity(); if (controlEntityData(entidad)) { String mnsj; if (isNew) { mnsj = DBAccess.AdministradoresORM.InsertaEntidad(entidad); } else { mnsj = DBAccess.AdministradoresORM.ModificaEntidad(entidad); } // Si existe un error se genera mensaje if (!mnsj.Equals("")) { MessageBox.Show(mnsj, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("Datos almacenados correctamente...", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information); loadListsDataToForm(); } } isModified = false; }
// - - - - - MODIFICA una entidad el la tabla public static string ModificaEntidad(DSKTUSERS entidad) { DSKTUSERS e = DBAccess.ORM.dbe.DSKTUSERS.Find(entidad.id); e = entidad; return(DBAccess.ORM.SaveChanges()); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CONTROL on EXIT WITHOUT SAVE private void Administradores_FormClosing(object sender, FormClosingEventArgs e) { if (isModified) { DSKTUSERS entidad = asignDataFormToEntity(); String mnsj = "Se ha modificado contenido y no ha sido grabado, desea guardar la información ??"; DialogResult isOK = MessageBox.Show(mnsj, "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (isOK == DialogResult.Yes) { almacenarDatosEntidad(); } } }
// - - - - - retorna DSKTUSERS por usuario y password public static DSKTUSERS LoginDsktUser(String user, String pass) { String hello = Publica.getHashString(pass); DSKTUSERS retu = null; List <DSKTUSERS> _entidades = (from e in DBAccess.ORM.dbe.DSKTUSERS where e.nickname.Equals(user) && e.password.Equals(hello) select e ).ToList(); if (_entidades != null && _entidades.Count > 0) { retu = _entidades[0]; } return(retu); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - GENERA ENTIDAD VACIA private DSKTUSERS generateEmptyEntity() { DSKTUSERS entidad = new DSKTUSERS(); entidad.id = 0; entidad.estado = 0; entidad.nickname = ""; entidad.password = ""; entidad.nombre = ""; entidad.email = ""; entidad.idprovincia = 0; entidad.idccaa = 0; entidad.iddelegacion = 0; entidad.ctrlmaster = 0; return(entidad); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - DATOS ENTIDAD a FORM private void asignDataEntityToForm(DSKTUSERS entidad) { if (entidad != null) { textBoxId.Text = entidad.id.ToString(); textBoxUsuario.Text = entidad.nickname; textBoxClave.Text = entidad.password; if (entidad.estado == 1) { checkBoxActivado.Checked = true; } else { checkBoxActivado.Checked = false; } textBoxNombre.Text = entidad.nombre; textBoxEmail.Text = entidad.email; if (entidad.idccaa > 0) { comboBoxComunidad.SelectedValue = entidad.idccaa; comboBoxProvincia.SelectedValue = entidad.idprovincia; } else { comboBoxComunidad.SelectedIndex = 0; bindingSourceProvincias.DataSource = ((CCAA)comboBoxComunidad.SelectedItem).PROVINCIAS.ToList(); } if (entidad.iddelegacion > 0) { comboBoxDelegacion.SelectedValue = entidad.iddelegacion; } else { comboBoxDelegacion.SelectedIndex = 0; } if (entidad.ctrlmaster == 1) { checkBoxMaster.Checked = true; } else { checkBoxMaster.Checked = false; } } isModified = false; controlPswrd = false; }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - BOTON ELIMINAR private void buttonEliminar_Click(object sender, EventArgs e) { DSKTUSERS entidad = (DSKTUSERS)listBoxLista.SelectedItem; if (entidad != null) { String mnsj = "Está seguro de eliminar definitivamente " + entidad.nickname + " ?"; DialogResult isOK = MessageBox.Show(mnsj, "Atención", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (isOK == DialogResult.Yes) { mnsj = DBAccess.AdministradoresORM.DeleteEntidad(entidad); // Si existe un error se genera mensaje if (!mnsj.Equals("")) { MessageBox.Show(mnsj, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } loadListsDataToForm(); loadInitialSelectedData(); } isModified = false; }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CONTROL DATOS ENTIDAD private bool controlEntityData(DSKTUSERS entidad) { bool isOK = true; string mnsj = ""; // Si nombre vacio, genera flag de error y mensaje if (isOK && entidad.nickname.Length <= 0) { isOK = false; mnsj = "El nombre no puede estar vacío."; textBoxUsuario.Focus(); } // Si clave vacio, genera flag de error y mensaje if (isOK && entidad.password.Length <= 0) { isOK = false; mnsj = "La clave no puede estar vacía."; textBoxClave.Focus(); } // Si existe un error se genera mensaje if (!isOK) { MessageBox.Show(mnsj, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(isOK); }
// - - - - - ELIMINA una entidad de la tabla public static string DeleteEntidad(DSKTUSERS entidad) { ORM.dbe.DSKTUSERS.Remove(entidad); return(DBAccess.ORM.SaveChanges()); }
// - - - - - INSERTA una entidad el la tabla public static string InsertaEntidad(DSKTUSERS entidad) { ORM.dbe.DSKTUSERS.Add(entidad); return(DBAccess.ORM.SaveChanges()); }