protected void gridVacas_RowDeleting(object sender, GridViewDeleteEventArgs e) { // Logger variables System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(true); System.Diagnostics.StackFrame stackFrame = new System.Diagnostics.StackFrame(); string className = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name; string methodName = stackFrame.GetMethod().Name; int Usuario_ID = Convert.ToInt32(gridVacas.DataKeys[e.RowIndex].Value); using (ChevacaDB context = new ChevacaDB()) { vacas obj = context.vacas.First(x => x.Vaca_ID == Usuario_ID); context.vacas.Remove(obj); context.SaveChanges(); #region Save log try { string userID1 = HttpContext.Current.Session["UserID"].ToString(); string username = HttpContext.Current.Session["UserName"].ToString(); Global_Objects.Logs.AddUserLog("Borra vacas", obj.GetType().Name + ": " + obj.Vaca_ID, userID1, username); } catch (Exception ex) { Global_Objects.Logs.AddErrorLog("Excepcion. Guardando log. ERROR:", className, methodName, ex.Message); } #endregion BindGrid(); lblMessage.Text = "Borrado correctamente."; } }
protected void gridVacas_RowCommand(object sender, GridViewCommandEventArgs e) { // Logger variables System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(true); System.Diagnostics.StackFrame stackFrame = new System.Diagnostics.StackFrame(); string className = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name; string methodName = stackFrame.GetMethod().Name; if (e.CommandName == "InsertNew") { GridViewRow row = gridVacas.FooterRow; TextBox txb1 = row.FindControl("txbNew1") as TextBox; TextBox txb2 = row.FindControl("txbNew2") as TextBox; TextBox txb3 = row.FindControl("txbNew3") as TextBox; if (txb1 != null && txb2 != null && txb3 != null) { using (ChevacaDB context = new ChevacaDB()) { #region Agregar registro vacas obj = new vacas(); obj.Nombre = txb1.Text; int Estancia_ID = 0; if (!int.TryParse(txb2.Text, out Estancia_ID)) { Estancia_ID = 0; Logs.AddErrorLog("Excepcion. Convirtiendo int. ERROR:", className, methodName, txb2.Text); } obj.Estancia_ID = Estancia_ID; obj.MGAP_ID = txb3.Text; context.vacas.Add(obj); context.SaveChanges(); #endregion #region Save log try { int id = 1; vacas vacas = (vacas)context.vacas.OrderByDescending(p => p.Vaca_ID).FirstOrDefault(); if (vacas != null) { id = vacas.Vaca_ID; } string userID1 = HttpContext.Current.Session["UserID"].ToString(); string username = HttpContext.Current.Session["UserName"].ToString(); Global_Objects.Logs.AddUserLog("Agrega vacas", vacas.GetType().Name + ": " + id, userID1, username); } catch (Exception ex) { Global_Objects.Logs.AddErrorLog("Excepcion. Guardando log. ERROR:", className, methodName, ex.Message); } #endregion lblMessage.Text = "Agregado correctamente."; BindGrid(); } } else { ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('Por favor ingrese el Nombre');", true); } } else if (e.CommandName == "View") { string[] values = e.CommandArgument.ToString().Split(new char[] { ',' }); if (values.Length > 1) { string tabla = values[0]; string dato = values[1]; if (!string.IsNullOrWhiteSpace(tabla) && !string.IsNullOrWhiteSpace(dato)) { Response.Redirect("Listados.aspx?tabla=" + tabla + "&dato=" + dato); } } } else { //BindGrid(); } }
protected void gridVacas_RowUpdating(object sender, GridViewUpdateEventArgs e) { // Logger variables System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(true); System.Diagnostics.StackFrame stackFrame = new System.Diagnostics.StackFrame(); string className = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name; string methodName = stackFrame.GetMethod().Name; GridViewRow row = gridVacas.Rows[e.RowIndex]; TextBox txb1 = row.FindControl("txb1") as TextBox; TextBox txb2 = row.FindControl("txb2") as TextBox; TextBox txb3 = row.FindControl("txb3") as TextBox; if (txb1 != null && txb2 != null && txb3 != null) { using (ChevacaDB context = new ChevacaDB()) { #region Update registro //vacas obj = (vacas)(e.Row.DataItem); int id = Convert.ToInt32(gridVacas.DataKeys[e.RowIndex].Value); vacas obj = context.vacas.FirstOrDefault(x => x.Vaca_ID == id); if (obj != null) { obj.Nombre = txb1.Text; int Estancia_ID = obj.Estancia_ID; if (!int.TryParse(txb2.Text, out Estancia_ID)) { Estancia_ID = obj.Estancia_ID; Logs.AddErrorLog("Excepcion. Convirtiendo int. ERROR:", className, methodName, txb2.Text); } obj.Estancia_ID = Estancia_ID; obj.MGAP_ID = txb3.Text; context.SaveChanges(); } #endregion context.SaveChanges(); #region Save log try { string userID1 = HttpContext.Current.Session["UserID"].ToString(); string username = HttpContext.Current.Session["UserName"].ToString(); Global_Objects.Logs.AddUserLog("Modifica vacas", obj.GetType().Name + ": " + obj.Vaca_ID, userID1, username); } catch (Exception ex) { Global_Objects.Logs.AddErrorLog("Excepcion. Guardando log. ERROR:", className, methodName, ex.Message); } #endregion lblMessage.Text = "Guardado correctamente."; gridVacas.EditIndex = -1; BindGrid(); } } }