/// <summary> /// Busca en la base de datos el producto y carga su información en el formulario /// </summary> private void CargarCampos() { //Sesión de conexión a la base de datos var cfg = new Configuration(); cfg.Configure(); var sessions = cfg.BuildSessionFactory(); var sess = sessions.OpenSession(); //Consulta a la BD IQuery q = sess.CreateQuery("FROM Producto where idProducto=" + idProducto); var resultados = q.List<Producto>(); //Llenar los campos producto = resultados[0]; txtNuevoDescripcion.Text = producto.descripcion; txtNuevoExistencia.Text = producto.existencia.ToString(); txtNuevoPrecioProveedor.Text = producto.precioProveedor.ToString(CultureInfo.InvariantCulture); txtNuevoPrecioUnitario.Text = producto.precioUnitario.ToString(CultureInfo.InvariantCulture); rutaFinal = producto.foto; if (producto.consignacion.Equals("La Modistería")) txtConsignacion.SelectedIndex = 0; else { var consignacionVal = proveedorBox; consignacionVal.Text = producto.consignacion; txtConsignacion.SelectedIndex = 1; } }
/// <summary> /// Registra el producto en la base de datos /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RegistrarProducto(object sender, RoutedEventArgs e) { bool valido = true; float a; int b=0; //Validación de los campos if (txtNombre.Text == "") { txtNombre.Text = "*"; valido = false; } if (txtPrecioProveedor.Text == "" || !float.TryParse(txtPrecioProveedor.Text, out a)) { txtPrecioProveedor.Text = "*"; valido = false; } if (txtPrecioUnitario.Text == "" || !float.TryParse(txtPrecioUnitario.Text, out a)) { txtPrecioUnitario.Text = "*"; valido = false; } if (txtExistencia.Text == "" || !int.TryParse(txtExistencia.Text, out b)) { txtExistencia.Text = "*"; valido = false; } //Adquisición de la imagen if (valido) { try { var imageFile = new System.IO.FileInfo(rutaFotoProducto); if (imageFile.Exists) { //Conseguir el directorio local var applicationPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); Console.WriteLine(applicationPath); //Adquirir ruta local de carpeta de Fotografias var dir = new System.IO.DirectoryInfo(System.IO.Path.Combine(applicationPath, "RepProductos")); if (!dir.Exists) dir.Create(); rutaFinal = String.Format("{0}\\{1}", dir.FullName, nombreImagen); Console.WriteLine(rutaFinal); //Copiar imagen a la carpeta de Fotografias imageFile.CopyTo(System.IO.Path.Combine(dir.FullName, nombreImagen), true); } } catch (Exception ) { } //Registro del producto en la base de datos //Conversión a flotante válido de los precios float pu = Convert.ToSingle(txtPrecioUnitario.Text, CultureInfo.InvariantCulture); float pv = Convert.ToSingle(txtPrecioProveedor.Text, CultureInfo.InvariantCulture); //Obtención del proveedor string consignacionVal=""; switch (txtConsignacion.SelectedIndex) { case 0: consignacionVal = "La Modistería"; break; case 1: var consignacionBox = proveedorBox; consignacionVal = consignacionBox.Text; break; } //Creación de la instancia Producto nuevoProducto = new Producto { nombre = txtNombre.Text, precioUnitario = pu, precioProveedor = pv, consignacion = consignacionVal, existencia=b, descripcion=txtDescripcion.Text, foto=rutaFinal }; //Almacenamiento en la base de datos var cfg = new Configuration(); cfg.Configure(); var sessions = cfg.BuildSessionFactory(); var sess = sessions.OpenSession(); sess.Save(nuevoProducto); sess.Flush(); sess.Close(); //Reset de campos y carga de registros ResetCampos(); CargarProductos(); } else MessageBox.Show("Alguno(s) de los campos son inválidos", "La Modistería | ERROR"); }