public static string AltaHotel(int id_ciudad, string nombre, string cif, int categoria, string direccion, int telefono, string tipo) { string mensaje = ""; hoteles h = new hoteles(); h.id_ciudad = id_ciudad; h.nombre = nombre; h.cif = cif; h.categoria = categoria; h.direccion = direccion; h.telefono = telefono; h.tipo = tipo; contexto.hoteles.Add(h); try { contexto.SaveChanges(); } catch (DbUpdateException ex) { SqlException sqlEx = (SqlException)ex.InnerException.InnerException; mensaje = BDErrores.MensajeError(sqlEx); contexto.hoteles.Remove(h); } return(mensaje); }
// - - - - - inserta un hotel el la tabla public static string ModificaHotel(hoteles hotel) { hoteles h = DBData.ORM.dbh.hoteles.Find(hotel.id_ciudad, hotel.nombre); h = hotel; return(DBData.ORM.SaveChanges()); }
private void dataGridViewHoteles_DoubleClick(object sender, DataGridViewCellEventArgs e) { hoteles hotel = (hoteles)dataGridViewHoteles.CurrentRow.DataBoundItem; FormHotel frm = new FormHotel(hotel); frm.Text = "Modificación hotel"; frm.ShowDialog(); // bindingSourceCiudades.DataSource = DBData.CiudadORM.SelectAllCiudades(); bindingSourceHoteles.DataSource = GetBySelectedCity(); }
private void dataGridViewHoteles_DeleteRow(object sender, DataGridViewRowCancelEventArgs e) { hoteles hotel = (hoteles)dataGridViewHoteles.CurrentRow.DataBoundItem; String mnsj = "Está seguro de eliminar definitivamente el hotel " + hotel.nombre + " ?"; DialogResult isOK = MessageBox.Show(mnsj, "Atención", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (isOK == DialogResult.OK) { mnsj = DBData.HotelORM.DeleteHotel(hotel); if (!mnsj.Equals("")) { MessageBox.Show(mnsj, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } bindingSourceCiudades.DataSource = DBData.CiudadORM.SelectAllCiudades(); bindingSourceHoteles.DataSource = GetBySelectedCity(); }
private void buttonAceptar_Click(object sender, EventArgs e) { int aux; string errmsg = "", ormmsg = ""; bool isOK = true; bool isNew = true; if (m_hotel != null) { isNew = false; } else { m_hotel = new hoteles(); } // El nom de l’hotel ha de ser obligatori. m_hotel.nombre = textBoxNombre.Text; if (isOK && m_hotel.nombre.Equals("")) { isOK = false; errmsg = "El nombre no puede estar vacío"; textBoxNombre.Focus(); } // Control de categoria if (isOK && int.TryParse(textBoxCategoria.Text, out aux)) { m_hotel.categoria = int.Parse(textBoxCategoria.Text); if (m_hotel.categoria < 1 || m_hotel.categoria > 5) { isOK = false; errmsg = "Categoría debe ser entre 1 y 5"; textBoxCategoria.Focus(); } } else { isOK = false; errmsg = "Error de formato de Categoría"; textBoxCategoria.Focus(); } // Control de longitud de cadena de numero de telefono if (isOK && (textBoxTelefono.TextLength > 0 && textBoxTelefono.TextLength <= 9)) { if (int.TryParse(textBoxTelefono.Text, out aux)) { m_hotel.telefono = int.Parse(textBoxTelefono.Text); } else { isOK = false; errmsg = "Error de formatyo de teléfono"; textBoxCategoria.Focus(); } } else { isOK = false; errmsg = "El número de teréfono es demasiado extenso"; textBoxTelefono.Focus(); } m_hotel.direccion = textBoxDireccion.Text; m_hotel.tipo = comboBoxTipo.SelectedItem.ToString(); m_hotel.id_ciudad = ((ciudades)comboBoxCiudades.SelectedItem).id_ciudad; m_hotel.cif = ((cadenas)comboBoxCadena.SelectedItem).cif; // Si les dades són correctes: if (isOK) { if (isNew) { // Si estem tractant un alta: Donar d’alta un hotel amb les dades introduïdes per l’usuari. En cas d’error donar el missatge corresponent.Si no, donar el missatge de que s’ha realitzat correctament l’alta. ormmsg = DBData.HotelORM.InsertaHotel(m_hotel); } else { // Si estem tractant una modificació: // Modificar l’hotel amb les dades introduïdes per l’usuari. ormmsg = DBData.HotelORM.ModificaHotel(m_hotel); } this.Close(); } else { // Si les dades són incorrectes, donar el missatge adient i posicionar el cursor en el camp erroni. MessageBox.Show(errmsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (!ormmsg.Equals("")) { // En cas d’error donar el missatge corresponent.Si no, donar el missatge de que s’ha realitzat correctament la modificació. MessageBox.Show(ormmsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public FormHotel(hoteles hotel) { InitializeComponent(); this.m_hotel = hotel; }
// - - - - - elimina un hotel de la tabla public static string DeleteHotel(hoteles hotel) { ORM.dbh.hoteles.Remove(hotel); return(DBData.ORM.SaveChanges()); }
// - - - - - inserta un hotel el la tabla public static string InsertaHotel(hoteles hotel) { ORM.dbh.hoteles.Add(hotel); return(DBData.ORM.SaveChanges()); }