private void buttonGuardar_Click(object sender, EventArgs e) { try { if (String.IsNullOrWhiteSpace(this.textBoxNombreProducto.Text) || String.IsNullOrWhiteSpace(this.comboBoxCategoria.Text)) { MensajeError("Los campos Nombre de Producto y Categoría son obligatorios"); } else { if (String.IsNullOrWhiteSpace(this.textBoxPrecioUnitario.Text)) { this.textBoxPrecioUnitario.Text = "0"; } String mensaje = NProductos.Insertar(this.textBoxNombreProducto.Text, this.comboBoxCategoria.Text, Convert.ToDecimal(this.textBoxPrecioUnitario.Text), this.textBoxDetalles.Text); if (mensaje == "Y") { this._owner.Mensaje(String.Format("El Producto {0} ha sido AGREGADO", this.textBoxNombreProducto.Text)); this._owner.Refrescar(); this.Close(); } else { MensajeError(mensaje); } } } catch (Exception ex) { MensajeError("Los valores ingresados son incorrectos. " + ex.Message); } }
//btnAgregarActualizar - Evento Click - Inserta o edita un usuario de la base de datos. private void btnAgregarActualizar_Click(object sender, EventArgs e) { string Respuesta; decimal Costo; decimal Utilidad; decimal Existencia; if (this.lblTitulo.Text == "Nuevo Producto") { //Revisión de datos obligatorios if (String.IsNullOrWhiteSpace(txtDescripcion.Text)) { MessageBox.Show("Debe ingresar la descripción del producto.", String.Format(Configuracion.Titulo, "Dato Inválido"), MessageBoxButtons.OK, MessageBoxIcon.Warning); this.errorProvider.SetError(txtDescripcion, "Ingrese una descripción."); } else if (String.IsNullOrWhiteSpace(txtPrecioBase.Text)) { MessageBox.Show("Debe ingresar un precio libre de impuestos.", String.Format(Configuracion.Titulo, "Dato Inválido"), MessageBoxButtons.OK, MessageBoxIcon.Warning); this.errorProvider.SetError(txtPrecioBase, "Ingrese un precio libre de impuestos."); } else if (String.IsNullOrWhiteSpace(txtPrecioVenta.Text)) { MessageBox.Show("Debe ingresar un precio de venta.", String.Format(Configuracion.Titulo, "Dato Inválido"), MessageBoxButtons.OK, MessageBoxIcon.Warning); this.errorProvider.SetError(txtPrecioBase, "Ingrese un precio de venta."); } else { //Verifica si el producto es exento y extablece la normativa "(E.)" if (cbxImpuesto.Text == "EXENTO 00.00 %") { txtDescripcion.Text += " (E.)"; } //Verifica que los campos de Utilidad y costo hayan sido rellendados if (!String.IsNullOrEmpty(txtCosto.Text)) { Costo = Convert.ToDecimal(txtCosto.Text); } else { Costo = 0; } if (!String.IsNullOrEmpty(txtUtilidad.Text)) { Utilidad = Convert.ToDecimal(txtUtilidad.Text.Replace("%", "")); } else { Utilidad = 0; } if (!String.IsNullOrEmpty(txtExistencia.Text)) { Existencia = Convert.ToDecimal(txtExistencia.Text); } else { Existencia = 0; } //Ingresa los datos en la base de datos. Respuesta = NProductos.Insertar(txtDescripcion.Text, Costo, Convert.ToInt32(cbxImpuesto.SelectedValue), Utilidad, Convert.ToDecimal(txtPrecioBase.Text), Convert.ToDecimal(txtPrecioVenta.Text), Existencia, Convert.ToInt32(cbxCategoria.SelectedValue), this.ctrlProductos.IdUsuario); if (Respuesta == "OK") { //Muestra confirmación al usuario via MessageBox. MessageBox.Show("El producto fue ingresado en el sistema satisfactoriamente.", String.Format(Configuracion.Titulo, "Registro Exitoso"), MessageBoxButtons.OK, MessageBoxIcon.Information); ctrlProductos.Mostrar(); ctrlProductos.Mensaje(String.Format("El producto {0} ha sido AGREGADO satisfactoriamente. ", txtDescripcion.Text)); this.Close(); } else { //Muestra Respuesta error al usuario mediante MessageBox MessageBox.Show(Respuesta, String.Format(Configuracion.Titulo, "Error"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else if (this.lblTitulo.Text == "Editar Cliente") { //Verifica si el producto es exento y extablece la normativa "(E.)" if (cbxImpuesto.Text == "EXENTO 00.00 %") { txtDescripcion.Text += " (E.)"; } //Revisión de datos obligatorios. if (String.IsNullOrWhiteSpace(txtDescripcion.Text)) { MessageBox.Show("Debe ingresar la descripción del producto.", String.Format(Configuracion.Titulo, "Dato Inválido"), MessageBoxButtons.OK, MessageBoxIcon.Warning); this.errorProvider.SetError(txtDescripcion, "Ingrese una descripción."); } else if (String.IsNullOrWhiteSpace(txtPrecioBase.Text)) { MessageBox.Show("Debe ingresar un precio libre de impuestos.", String.Format(Configuracion.Titulo, "Dato Inválido"), MessageBoxButtons.OK, MessageBoxIcon.Warning); this.errorProvider.SetError(txtPrecioBase, "Ingrese un precio libre de impuestos."); } else if (String.IsNullOrWhiteSpace(txtPrecioVenta.Text)) { MessageBox.Show("Debe ingresar un precio de venta.", String.Format(Configuracion.Titulo, "Dato Inválido"), MessageBoxButtons.OK, MessageBoxIcon.Warning); this.errorProvider.SetError(txtPrecioBase, "Ingrese un precio de venta."); } else { //Verifica si el producto es exento y extable la normativa "(E.)" if (cbxImpuesto.Text == "EXENTO") { txtDescripcion.Text = txtDescripcion.Text.Replace(" (E.)", "") + " (E.)"; } //Verifica que los campos de Utilidad y costo hayan sido rellendados if (!String.IsNullOrEmpty(txtCosto.Text)) { Costo = Convert.ToDecimal(txtCosto.Text); } else { Costo = 0; } if (!String.IsNullOrEmpty(txtUtilidad.Text)) { Utilidad = Convert.ToDecimal(txtUtilidad.Text.Replace("%", "")); } else { Utilidad = 0; } if (!String.IsNullOrEmpty(txtExistencia.Text)) { Existencia = Convert.ToDecimal(txtExistencia.Text); } else { Existencia = 0; } Respuesta = NProductos.Editar(this.IdProducto, txtDescripcion.Text, Costo, Convert.ToInt32(cbxImpuesto.SelectedValue), Utilidad, Convert.ToDecimal(txtPrecioBase.Text), Convert.ToDecimal(txtPrecioVenta.Text), Existencia, Convert.ToInt32(cbxCategoria.SelectedValue), this.ctrlProductos.IdUsuario); if (Respuesta == "OK") { //Muestra confirmación al usuario via MessageBox. MessageBox.Show(String.Format("Los datos del producto {0} fueron modificados satisfactoriamente.", txtDescripcion.Text), String.Format(Configuracion.Titulo, "Actualización de Datos Exitosa"), MessageBoxButtons.OK, MessageBoxIcon.Information); ctrlProductos.Mostrar(); ctrlProductos.Mensaje(String.Format("Los Datos del producto {0} fueron modificados satisfactoriamente.", txtDescripcion.Text)); this.Close(); } else { //Muestra Respuesta error al usuario mediante MessageBox. MessageBox.Show(Respuesta, String.Format(Configuracion.Titulo, "Error"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }