private void btnInsertarNoticia_Click(object sender, EventArgs e) { if (txtTitulo.Text.ToString() == "" || cbCategoria.SelectedIndex == -1 || txtTitulo.Text.ToString() == "" || txtImage.Text.ToString() == "") { MessageBox.Show("Introduzca todos los campos", "Error en los campos", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { b.Conectar(); string titulo = txtTitulo.Text.ToString(); string text = txtTexto.Text.ToString(); string url = txtImage.Text.ToString(); int id_categoria = listaCategorias[cbCategoria.SelectedIndex].Id; noticie n = new noticie(titulo, id_categoria, text, url, id_user); if (b.InsertarPublicacion(n)) { MessageBox.Show("Se ha insertado correctamente", "Insertado correcto", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Ha ocurrido un error al insertar", "Error al insertar", MessageBoxButtons.OK, MessageBoxIcon.Error); } } b.Desconectar(); txtTitulo.Text = ""; txtImage.Text = ""; txtTexto.Text = ""; }
private void btnModificar_Click(object sender, EventArgs e) { if (txtTitulo.Text == "" || txtTexto.Text == "" || txtImage.Text == "" || txtTitulo.Text == "" || txtId_user.Text == "") { MessageBox.Show("Completa todos los campos", "Campos incompletos", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { noticie modificada = new noticie(); modificada.Id = Convert.ToInt32(txtIdNoticia.Text); modificada.Title = txtTitulo.Text; modificada.Text = txtTexto.Text; modificada.Image = txtImage.Text; modificada.Category = listaCategorias[cbCategoria.SelectedIndex].Id; modificada.Id_user = Convert.ToInt32(txtId_user.Text); b.Conectar(); if (b.ModificarPublicacion(modificada)) { MessageBox.Show("Se ha modificado correctamente", "Modificación comletada", MessageBoxButtons.OK, MessageBoxIcon.Information); cbNoticias.Items.Clear(); listaNoticias = b.ListaPublicaciones(); foreach (noticie noticie in listaNoticias) { cbNoticias.Items.Add(noticie.Title); } } else { MessageBox.Show("Ha ocurrido un error al modificar", "Error al modificar", MessageBoxButtons.OK, MessageBoxIcon.Error); } b.Desconectar(); txtIdNoticia.Text = ""; txtTitulo.Text = ""; txtTexto.Text = ""; txtImage.Text = ""; txtId_user.Text = ""; cbCategoria.SelectedIndex = -1; } }
public bool ModificarPublicacion(noticie noticie) { MySqlCommand c = new MySqlCommand(); MySqlDataReader d; try { c.Connection = cnx; c.CommandText = "UPDATE noticies SET title = '" + noticie.Title + "', id_category = '" + noticie.Category + "', text = '" + noticie.Text + "', image = '" + noticie.Image + "' WHERE id = '" + noticie.Id + "'"; if (c.ExecuteNonQuery() == 1) { return(true); } else { return(false); } } catch (MySqlException ex) { return(false); } }
public bool InsertarPublicacion(noticie noticia) { MySqlCommand c = new MySqlCommand(); try { c.Connection = cnx; c.CommandText = "INSERT INTO `noticies` (`id`, `title`, `id_category`, `text`, `image`, `id_user`) VALUES (NULL, '" + noticia.Title + "', '" + noticia.Category + "', '" + noticia.Text + "', '" + noticia.Image + "', '" + noticia.Id_user + "')"; if (c.ExecuteNonQuery() == 1) { return(true); } else { return(false); } } catch (MySqlException ex) { return(false); } }