protected void btnGuardar_Click(object sender, EventArgs e) { decimal capacidadKG = 0; decimal restanteKG = 0; try { capacidadKG = Convert.ToDecimal(CapacidadTextBox.Text.Trim()); restanteKG = Convert.ToDecimal(DisponibleTextBox.Text.Trim()); } catch { Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "ShowMensaje('error', 'El campo CapacidadKG solo acepta nĂºmeros.')", true); return; } Almacen obj = new Almacen() { Descripcion = DescripcionTextBox.Text.Trim(), CapacidadKG = capacidadKG, RestanteKG = restanteKG, ProductoId = string.IsNullOrEmpty(ProductosList.SelectedValue) ? 0 : Convert.ToInt32(ProductosList.SelectedValue) }; try { if (string.IsNullOrEmpty(AlmacenIdHD.Value)) { AlmacenBLL.InsertAlmacen(obj); } else { decimal lastCapacidadKG = Convert.ToDecimal(LastCapacidadKGHD.Value); obj.AlmacenId = Convert.ToInt32(AlmacenIdHD.Value); decimal usadosKG = lastCapacidadKG - restanteKG; if (capacidadKG <= usadosKG) { Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "ShowMensaje('warning', 'No puede registrar una capacidad menor al espacio ya ocupado.')", true); return; } obj.RestanteKG = obj.CapacidadKG - usadosKG; AlmacenBLL.UpdateAlmacen(obj); } } catch (Exception q) { Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "ShowMensaje('error', 'Error al registrar Almacen.')", true); } Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "ShowMensaje('success', 'Registro Exitoso.')", true); ClearAlmacen(); Response.Redirect("ListaAlmacen.aspx"); }