protected void btn_buscar_Click(object sender, EventArgs e) { try { if (ddl_seleccionar.SelectedIndex == 0) { Response.Write("<script>alert('Debe asociar la imagen a un Proyecto, Servicio o Empresa.');</script>"); } else if (ddl_seleccionar.SelectedIndex == 1) { if (ddl_nombrePro.SelectedIndex != 0) { MantenedorFotosProyectos man = new MantenedorFotosProyectos(); string nombre = ddl_nombrePro.SelectedValue; txt_id.Text = man.BuscarFoto(nombre).Rows[0][0].ToString(); txt_descripcion.Text = man.BuscarFoto(nombre).Rows[0][1].ToString(); ddl_pro.Text = man.BuscarFoto(nombre).Rows[0][2].ToString(); txt_nombreimagen.Text = man.BuscarFoto(nombre).Rows[0][4].ToString(); SqlConnection sql = new SqlConnection(Conexion.CadenaDeConexion()); string query = "select imagen from fotoproyecto where nombreimagen='" + nombre + "'"; SqlCommand cm = new SqlCommand(query, sql); sql.Open(); cm.CommandTimeout = 0; byte[] img = (byte[])cm.ExecuteScalar(); System.Drawing.Image rImg = null; using (MemoryStream ms = new MemoryStream(img)) { rImg = System.Drawing.Image.FromStream(ms); imgPreview.ImageUrl = "data:image;base64," + Convert.ToBase64String(img); } } else { Response.Write("<script>alert('Debe seleccionar una imagen.');</script>"); } } else if (ddl_seleccionar.SelectedIndex == 2) { if (ddl_nombreServ.SelectedIndex != 0) { MantenedorFotosServicios man = new MantenedorFotosServicios(); string nombre = ddl_nombreServ.SelectedValue; txt_id.Text = man.BuscarFotoServicio(nombre).Rows[0][0].ToString(); txt_descripcion.Text = man.BuscarFotoServicio(nombre).Rows[0][1].ToString(); ddl_ser.Text = man.BuscarFotoServicio(nombre).Rows[0][3].ToString(); txt_nombreimagen.Text = man.BuscarFotoServicio(nombre).Rows[0][4].ToString(); SqlConnection sql = new SqlConnection(Conexion.CadenaDeConexion()); string query = "select imagen from fotoServicio where nombreimagen='" + nombre + "'"; SqlCommand cm = new SqlCommand(query, sql); sql.Open(); cm.CommandTimeout = 0; byte[] img = (byte[])cm.ExecuteScalar(); System.Drawing.Image rImg = null; using (MemoryStream ms = new MemoryStream(img)) { rImg = System.Drawing.Image.FromStream(ms); imgPreview.ImageUrl = "data:image;base64," + Convert.ToBase64String(img); } } else { Response.Write("<script>alert('Debe seleccionar una imagen.');</script>"); } } else { if (ddl_nombreEmp.SelectedIndex != 0) { MantenedorFotosEmpresa man = new MantenedorFotosEmpresa(); string nombre = ddl_nombreEmp.SelectedValue; txt_id.Text = man.BuscarFotoEmpresa(nombre).Rows[0][0].ToString(); txt_descripcion.Text = man.BuscarFotoEmpresa(nombre).Rows[0][1].ToString(); ddl_emp.Text = man.BuscarFotoEmpresa(nombre).Rows[0][3].ToString(); txt_nombreimagen.Text = man.BuscarFotoEmpresa(nombre).Rows[0][4].ToString(); SqlConnection sql = new SqlConnection(Conexion.CadenaDeConexion()); string query = "select imagen from fotoEmpresa where nombreimagen='" + nombre + "'"; SqlCommand cm = new SqlCommand(query, sql); sql.Open(); cm.CommandTimeout = 0; byte[] img = (byte[])cm.ExecuteScalar(); System.Drawing.Image rImg = null; using (MemoryStream ms = new MemoryStream(img)) { rImg = System.Drawing.Image.FromStream(ms); imgPreview.ImageUrl = "data:image;base64," + Convert.ToBase64String(img); } } else { Response.Write("<script>alert('Debe seleccionar una imagen.');</script>"); } } } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } }
protected void boton_modificar_Click(object sender, EventArgs e) { try { if (ddl_seleccionar.SelectedIndex == 1) { if (txt_nombreimagen.Text.Trim().Length > 0 && txt_descripcion.Text.Trim().Length > 0 && ddl_pro.SelectedIndex != 0) { if (txt_descripcion.Text.Length <= 500) { FotoProyecto foto = new FotoProyecto(); MantenedorFotosProyectos man = new MantenedorFotosProyectos(); string id = txt_id.Text; foto.Proyecto.NombreProyecto = ddl_pro.SelectedValue; foto.NombreImagen = txt_nombreimagen.Text; foto.Descripcion = txt_descripcion.Text; if (archivofoto.HasFile) { int tamanio = archivofoto.PostedFile.ContentLength; byte[] ImagenOriginal = new byte[tamanio]; archivofoto.PostedFile.InputStream.Read(ImagenOriginal, 0, tamanio); Bitmap ImagenOriginalBinaria = new Bitmap(archivofoto.PostedFile.InputStream); SqlConnection cone = new SqlConnection(Conexion.CadenaDeConexion()); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "UPDATE fotoproyecto SET Imagen=@imagen where IdFotopro='" + id + "'"; //imagen cmd.Parameters.Add("@imagen", SqlDbType.Image).Value = ImagenOriginal; cmd.CommandType = CommandType.Text; cmd.Connection = cone; cone.Open(); cmd.ExecuteNonQuery(); string ImagenDataURL64 = "data:image;base64," + Convert.ToBase64String(ImagenOriginal); imgPreview.ImageUrl = ImagenDataURL64; man.ModificarFoto(foto, id); } else { man.ModificarFoto(foto, id); } Response.Write("<script>alert('Modificado exitosamente.');</script>"); txt_nombreimagen.Text = ""; txt_id.Text = ""; txt_descripcion.Text = ""; ddl_nombrePro.SelectedIndex = 0; } else { Response.Write("<script>alert('La descripcion no debe superar los 500 caracteres.');</script>"); } } else { Response.Write("<script>alert('Debe rellenar todos los campos.');</script>"); } } else if (ddl_seleccionar.SelectedIndex == 2) { if (txt_nombreimagen.Text.Trim().Length > 0 && txt_descripcion.Text.Trim().Length > 0 && ddl_ser.SelectedIndex != 0) { if (txt_descripcion.Text.Length <= 500) { FotoServicio foto = new FotoServicio(); MantenedorFotosServicios man = new MantenedorFotosServicios(); string id = txt_id.Text; foto.Servicio.NombreServicio = ddl_ser.SelectedValue; foto.NombreImagen = txt_nombreimagen.Text; foto.Descripcion = txt_descripcion.Text; if (archivofoto.HasFile) { int tamanio = archivofoto.PostedFile.ContentLength; byte[] ImagenOriginal = new byte[tamanio]; archivofoto.PostedFile.InputStream.Read(ImagenOriginal, 0, tamanio); Bitmap ImagenOriginalBinaria = new Bitmap(archivofoto.PostedFile.InputStream); SqlConnection cone = new SqlConnection(Conexion.CadenaDeConexion()); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "UPDATE fotoServicio SET Imagen=@imagen where IdFotoserv='" + id + "'"; //imagen cmd.Parameters.Add("@imagen", SqlDbType.Image).Value = ImagenOriginal; cmd.CommandType = CommandType.Text; cmd.Connection = cone; cone.Open(); cmd.ExecuteNonQuery(); string ImagenDataURL64 = "data:image;base64," + Convert.ToBase64String(ImagenOriginal); imgPreview.ImageUrl = ImagenDataURL64; man.ModificarFotoServicio(foto, id); } else { man.ModificarFotoServicio(foto, id); } Response.Write("<script>alert('Modificado exitosamente.');</script>"); txt_nombreimagen.Text = ""; txt_id.Text = ""; txt_descripcion.Text = ""; ddl_nombreServ.SelectedIndex = 0; } else { Response.Write("<script>alert('La descripcion no debe superar los 500 caracteres.');</script>"); } } else { Response.Write("<script>alert('Debe rellenar todos los campos.');</script>"); } } else if (ddl_seleccionar.SelectedIndex == 3) { if (txt_nombreimagen.Text.Trim().Length > 0 && txt_descripcion.Text.Trim().Length > 0 && ddl_emp.SelectedIndex != 0) { if (txt_descripcion.Text.Length <= 500) { FotoEmpresa foto = new FotoEmpresa(); MantenedorFotosEmpresa man = new MantenedorFotosEmpresa(); string id = txt_id.Text; foto.Empresa.NombreEmpresa = ddl_emp.SelectedValue; foto.NombreImagen = txt_nombreimagen.Text; foto.Descripcion = txt_descripcion.Text; if (archivofoto.HasFile) { int tamanio = archivofoto.PostedFile.ContentLength; byte[] ImagenOriginal = new byte[tamanio]; archivofoto.PostedFile.InputStream.Read(ImagenOriginal, 0, tamanio); Bitmap ImagenOriginalBinaria = new Bitmap(archivofoto.PostedFile.InputStream); SqlConnection cone = new SqlConnection(Conexion.CadenaDeConexion()); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "UPDATE fotoEmpresa SET Imagen=@imagen where IdFotoEmp='" + id + "'"; //imagen cmd.Parameters.Add("@imagen", SqlDbType.Image).Value = ImagenOriginal; cmd.CommandType = CommandType.Text; cmd.Connection = cone; cone.Open(); cmd.ExecuteNonQuery(); string ImagenDataURL64 = "data:image;base64," + Convert.ToBase64String(ImagenOriginal); imgPreview.ImageUrl = ImagenDataURL64; man.ModificarFotoEmpresa(foto, id); } else { man.ModificarFotoEmpresa(foto, id); } Response.Write("<script>alert('Modificados exitosamente.');</script>"); txt_nombreimagen.Text = ""; txt_id.Text = ""; txt_descripcion.Text = ""; ddl_nombreEmp.SelectedIndex = 0; } else { Response.Write("<script>alert('La descripcion no debe superar los 500 caracteres.');</script>"); } } else { Response.Write("<script>alert('Debe rellenar todos los campos.');</script>"); } } else { Response.Write("<script>alert('Debe asociar la imagen a un Proyecto, Servicio o Empresa.');</script>"); } } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } }
protected void boton_eliminar_Click(object sender, EventArgs e) { try { if (ddl_seleccionar.SelectedIndex == 1) { if (txt_nombreimagen.Text.Trim().Length > 0) { MantenedorFotosProyectos ManCont = new MantenedorFotosProyectos(); if (ManCont.BuscarFoto(txt_nombreimagen.Text).Rows.Count > 0) { FotoProyecto foto = new FotoProyecto(); foto.NombreImagen = txt_nombreimagen.Text; ManCont.EliminarFoto(foto); Response.Write("<script>alert('Eliminada correctamente.');</script>"); ddl_nombrePro.SelectedIndex = 0; txt_nombreimagen.Text = ""; txt_descripcion.Text = ""; ddl_pro.SelectedIndex = 0; imgPreview.ImageUrl = "https://pngimage.net/wp-content/uploads/2018/05/agregar-png.png"; } else { Response.Write("<script>alert('La imagen no existe.');</script>"); } } else { Response.Write("<script>alert('Debe ingresar el nombre de la imagen a eliminar.');</script>"); } } else if (ddl_seleccionar.SelectedIndex == 2) { if (txt_nombreimagen.Text.Trim().Length > 0) { MantenedorFotosServicios ManCont = new MantenedorFotosServicios(); if (ManCont.BuscarFotoServicio(txt_nombreimagen.Text).Rows.Count > 0) { FotoServicio foto = new FotoServicio(); foto.NombreImagen = txt_nombreimagen.Text; ManCont.EliminarFotoServicio(foto); Response.Write("<script>alert('Eliminada correctamente.');</script>"); ddl_nombreServ.SelectedIndex = 0; txt_nombreimagen.Text = ""; txt_descripcion.Text = ""; ddl_pro.SelectedIndex = 0; imgPreview.ImageUrl = "https://pngimage.net/wp-content/uploads/2018/05/agregar-png.png"; } else { Response.Write("<script>alert('La imagen no existe.');</script>"); } } else { Response.Write("<script>alert('Debe ingresar el nombre de la imagen a eliminar.');</script>"); } } else if (ddl_seleccionar.SelectedIndex == 3) { if (txt_nombreimagen.Text.Trim().Length > 0) { MantenedorFotosEmpresa ManCont = new MantenedorFotosEmpresa(); if (ManCont.BuscarFotoEmpresa(txt_nombreimagen.Text).Rows.Count > 0) { FotoEmpresa foto = new FotoEmpresa(); foto.NombreImagen = txt_nombreimagen.Text; ManCont.EliminarFotoEmpresa(foto); Response.Write("<script>alert('Eliminada correctamente.');</script>"); ddl_nombreEmp.SelectedIndex = 0; txt_nombreimagen.Text = ""; txt_descripcion.Text = ""; ddl_pro.SelectedIndex = 0; imgPreview.ImageUrl = "https://pngimage.net/wp-content/uploads/2018/05/agregar-png.png"; } else { Response.Write("<script>alert('La imagen no existe.');</script>"); } } else { Response.Write("<script>alert('Debe ingresar el nombre de la imagen a eliminar.');</script>"); } } else { Response.Write("<script>alert('Debe asociar la imagen a un Proyecto, Servicio o Empresa.');</script>"); } } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } }