crud de productos completado
This commit is contained in:
+53
-2
@@ -1,4 +1,5 @@
|
||||
using compabetov2.database.modelos;
|
||||
using compabetov2.database;
|
||||
using compabetov2.database.modelos;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
@@ -59,7 +60,57 @@ namespace compabetov2.admin
|
||||
{
|
||||
try {
|
||||
decimal precio = decimal.Parse(txtPrecio.Text);
|
||||
}catch(Exception ex)
|
||||
string nombre = txtNombre.Text;
|
||||
//en caso de que descida cambiar la imagen
|
||||
if (!imagePath.Equals(""))
|
||||
{
|
||||
Producto productoNuevo = new Producto(producto.idProducto, nombre, "", precio, imagePath, 0);
|
||||
if (service.editarProducto(productoNuevo))
|
||||
{
|
||||
//guardar imagen nueva
|
||||
string destinationFolder = Path.Combine(Application.StartupPath, "Resources");
|
||||
string destinationFile = Path.Combine(destinationFolder, Path.GetFileName(imagePath));
|
||||
File.Copy(imagePath, destinationFile, true);
|
||||
|
||||
ventanaPadre.llenarLayout();
|
||||
|
||||
|
||||
//eliminar imagen antigua
|
||||
string imagenEliminada = Path.Combine(destinationFolder, producto.img);
|
||||
//se pone a esperar 1 segundo antes de eliminar la imagen
|
||||
//esto para que al programa le de tiempo de dejar de utilizar la imagen y no lance una excepcion
|
||||
Timer timer = new Timer();
|
||||
timer.Interval = 1000; // 1 segundos
|
||||
timer.Tick += delegate (object sender2, EventArgs e2)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(imagenEliminada);
|
||||
timer.Stop();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Manejar la excepción de eliminación de archivo temporal
|
||||
Console.WriteLine("Error al eliminar archivo temporal: " + ex.Message);
|
||||
}
|
||||
};
|
||||
|
||||
Close();
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// si la imagen no es modificada
|
||||
Producto productoNuevo = new Producto(producto.idProducto, nombre, "", precio, producto.img, 0);
|
||||
if (service.editarProducto(productoNuevo))
|
||||
{
|
||||
ventanaPadre.llenarLayout();
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
MessageBox.Show("Por favor introduzca un precio valido", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user