private async Task Edit(FinancialStatementContext db, TReference existingItem, TReference item, LogParams logParams) { var logEntries = new List <LogParams>(); try { logEntries.AddRange(EditFunc(db, existingItem, item)); await db.SaveChangesAsync(); } catch (Exception ex) { if (SqlExceptionHelper.IsUniqueConstraintError(ex)) { throw new PublicException(UniqueConstraintErrorText, ex); } throw new PublicException(EditingErrorText, ex); } foreach (var logEntry in logEntries) { logEntry.ActionType = EditingActionType; logEntry.AdUsername = logParams.AdUsername; logEntry.RemoteAddress = logParams.RemoteAddress; await SaveLog(db, logEntry); } }
/// <summary> /// 提交当前单元操作的结果 /// </summary> /// <returns></returns> public async Task <int> CommitAsync(bool validateOnSaveEnabled = true) { if (IsCommitted) { return(0); } try { int result = await Context.SaveChangesAsync(validateOnSaveEnabled); IsCommitted = true; return(result); } catch (DbUpdateException e) { if (e.InnerException?.InnerException is SqlException) { SqlException sqlEx = e.InnerException.InnerException as SqlException; string msg = SqlExceptionHelper.GetSqlExceptionMessage(sqlEx.Number); throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + msg, sqlEx); } throw; } }
protected override void ExecuteDelete() { try { base.ExecuteDelete(); } catch (Exception e) { SqlExceptionHelper.HandleDeleteForeignKeyException(e); throw; } }
protected override void ExecuteSave() { try { base.ExecuteSave(); } catch (Exception e) { SqlExceptionHelper.HandleSavePrimaryKeyException(e, fld.TerritoryID.Title); throw; } }
protected override void ExecuteSave() { try { base.ExecuteSave(); } catch (Exception e) { SqlExceptionHelper.HandleSavePrimaryKeyException(e, Localizer, fld.RegionID.GetTitle(Localizer)); throw; } }
public string InsertDataToDatabase(Handler siteInfo) { try { // Open Database Connection, Run Commands, Some additional Checks. } catch (SqlException exception) { SqlExceptionHelper errorCompare = new SqlExceptionHelper(exception); return(errorCompare.ToString()); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Label1.Text = "We will test the connection to the database."; try { SqlConnection connection = DataAccess.Connection.GetDBConnection(); connection.Close(); Label1.Text = "We successfully tested connection to the database"; } catch (SqlException ex) { Label1.Text = SqlExceptionHelper.GetErrorExplanation(ex); } } }
protected async Task Add(FinancialStatementContext db, TReference item, LogParams logParams) { try { AddFunc(db, item); await db.SaveChangesAsync(); } catch (Exception ex) { if (SqlExceptionHelper.IsUniqueConstraintError(ex)) { throw new PublicException(UniqueConstraintErrorText, ex); } throw new PublicException(CreatingErrorText, ex); } logParams.ActionType = CreatingActionType; logParams.Details = GetDetailsForLog(item); await SaveLog(db, logParams); }
private async Task EditMapping(FinancialStatementContext db, TMapping existingMapping, TViewModel model, LogParams logParams) { var logEntries = new List <LogParams>(); try { var existingModel = await GetSingleModel(db, model.RowId); var checkErrors = await CheckRestrictionsOnEdit(db, existingModel, model); if (checkErrors.Any()) { throw new PublicException("Mapping edit failed due to locks: " + JoinErrors(checkErrors)); } logEntries.AddRange(EditMappingFunc(db, existingMapping, existingModel, model)); existingMapping.ModifiedOn = DateTime.Now; existingMapping.ModifiedUser = logParams.AdUsername; await db.SaveChangesAsync(); } catch (Exception ex) { if (SqlExceptionHelper.IsUniqueConstraintError(ex)) { throw new PublicException(UniqueConstraintErrorText, ex); } throw new PublicException(EditingErrorText, ex); } foreach (var logEntry in logEntries) { logEntry.ActionType = EditingActionType; logEntry.AdUsername = logParams.AdUsername; logEntry.RemoteAddress = logParams.RemoteAddress; await SaveLog(db, logEntry); } }
private async Task AddMapping(FinancialStatementContext db, TViewModel model, LogParams logParams) { try { model.ModifiedOn = DateTime.Now; model.ModifiedUser = logParams.AdUsername; AddMappingFunc(db, model); await db.SaveChangesAsync(); } catch (Exception ex) { if (SqlExceptionHelper.IsUniqueConstraintError(ex)) { throw new PublicException(UniqueConstraintErrorText, ex); } throw new PublicException(CreatingErrorText, ex); } logParams.ActionType = CreatingActionType; logParams.Details = GetDetailsForLog(model); await SaveLog(db, logParams); }
public IEnumerable <Predicate <Exception> > GetExceptionPredicates() { #if NET5_0_OR_GREATER yield return(new Predicate <Exception>(e => e is SqlException exception && exception.IsTransient)); #endif yield return(new Predicate <Exception>(e => e is SqlException exception && SqlExceptionHelper.IsErrorNumberTransient(exception.Number))); yield return(new Predicate <Exception>(e => e is SqlException exception && exception.Number == 208)); //invalid object name }
protected void Page_Load(object sender, EventArgs e) { //if (!IsPostBack) //{ TableRow trow; TableCell tcellStockItemName, tcellSupplierName, tcellPackageType, tcellUnitPrice, tcellStockItemCheck, bcellUpdate, bcellDelete; try { using (DataSet dsStockItems = DataAccess.DataAccess.GetStockItems()) { DataTable dtStockItems = new DataTable(); for (int t = 0; t < dsStockItems.Tables.Count; t++) { dtStockItems = dsStockItems.Tables[t]; lblStockItemsCount.Text = dtStockItems.Rows.Count.ToString(); ddlStockItemName.DataSource = dtStockItems; ddlStockItemName.DataValueField = "StockItemName"; ddlStockItemName.DataTextField = "StockItemName"; ddlStockItemName.DataBind(); int i = 0; foreach (DataRow dtRow in dtStockItems.Rows) { trow = new TableRow(); tcellStockItemName = new TableCell(); tcellStockItemName.Controls.Add(RenderTextBox("StockItemName" + i.ToString(), dtRow["StockItemName"].ToString())); tcellSupplierName = new TableCell(); tcellSupplierName.Controls.Add(RenderTextBox("SupplierName" + i.ToString(), dtRow["SupplierName"].ToString())); tcellPackageType = new TableCell(); tcellPackageType.Controls.Add(RenderTextBoxSmall("PackageTypeName" + i.ToString(), dtRow["PackageTypeName"].ToString())); tcellUnitPrice = new TableCell(); tcellUnitPrice.Controls.Add(RenderTextBoxSmall("UnitPrice" + i.ToString(), dtRow["UnitPrice"].ToString())); tcellStockItemCheck = new TableCell(); tcellStockItemCheck.Controls.Add(RenderCheckBox("StockItemCheck" + i.ToString(), "")); trow.Cells.Add(tcellStockItemName); trow.Cells.Add(tcellSupplierName); trow.Cells.Add(tcellPackageType); trow.Cells.Add(tcellUnitPrice); trow.Cells.Add(tcellStockItemCheck); tblStoskItem.Rows.Add(trow); StockItem si = new StockItem(dtRow["StockItemName"].ToString(), dtRow["SupplierName"].ToString(), dtRow["PackageTypeName"].ToString(), dtRow["UnitPrice"].ToString()); sis.Add(si); i++; } } trow = new TableRow(); bcellUpdate = new TableCell(); bcellUpdate.Controls.Add(RenderButton("UpdateStockItems", "Update")); bcellDelete = new TableCell(); bcellDelete.Controls.Add(RenderButton("DeleteStockItems", "Delete")); trow.Cells.Add(bcellUpdate); trow.Cells.Add(bcellDelete); tblStoskItem.Rows.Add(trow); ds.Tables.Add(dtStockItems.Copy()); ds.Tables[0].TableName = "StockItems"; } } catch (SqlException ex) { Label1.Text = SqlExceptionHelper.GetErrorExplanation(ex); } catch (EmptyTableException ex) { lblStockItemsCount.Text = "0"; Label1.Text = ex.Message() + ex.Text(); Label1.ForeColor = System.Drawing.Color.Red; } }// Page_Load(object sender, EventArgs e)