public IHttpActionResult PostNewVoucher(VoucherViewModel voucher) { //if (!ModelState.IsValid) // return BadRequest("Invalid data."); using (var ctx = new dbInventoryEntities()) { int _intVoucherNo = 1; var obj = ctx.tblVouchers.OrderByDescending(t => t.intVoucherNo).FirstOrDefault(); if (obj != null) { _intVoucherNo = obj.intVoucherNo + 1; } ctx.tblVouchers.Add(new tblVoucher() { intVoucherNo = _intVoucherNo, dtDate = voucher.dtDate, intZoneName = voucher.intZoneName, intSOName = voucher.intSOName }); int _intId = 1; var obj1 = ctx.tblVoucherDTLs.OrderByDescending(t => t.intId).FirstOrDefault(); if (obj1 != null) { _intId = obj1.intId + 1; } foreach (var detail in voucher.voucherDTL) { var detailVoucher = new tblVoucherDTL { intId = _intId, intCategory = detail.intCategory, intVNo = _intVoucherNo, intItemCode = detail.intItemCode, intPieces = detail.intPieces, floatRate = detail.floatRate, floatCommission = detail.floatCommission, floatAmount = detail.floatAmount }; ctx.tblVoucherDTLs.Add(detailVoucher); _intId++; } try { ctx.SaveChanges(); } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } } return(Ok()); }
public IHttpActionResult Put(VoucherViewModel voucher) { //if (!ModelState.IsValid) // return BadRequest("Not a valid model"); using (var ctx = new dbInventoryEntities()) { try { var existingVoucher = ctx.tblVouchers.Where(s => s.intVoucherNo == voucher.intVoucherNo) .FirstOrDefault <tblVoucher>(); if (existingVoucher != null) { //existingVoucher.dtDate = voucher.dtDate; existingVoucher.intZoneName = voucher.intZoneName; existingVoucher.intSOName = voucher.intSOName; ctx.SaveChanges(); } int id = 0; foreach (var detail in voucher.voucherDTL) { int id1 = voucher.voucherDTL[id].intId; var existingVoucherDTL = ctx.tblVoucherDTLs.Where(s => s.intId == id1) .FirstOrDefault <tblVoucherDTL>(); if (existingVoucherDTL != null) { existingVoucherDTL.intCategory = detail.intCategory; existingVoucherDTL.intItemCode = detail.intItemCode; existingVoucherDTL.intPieces = detail.intPieces; existingVoucherDTL.floatRate = detail.floatRate; existingVoucherDTL.floatCommission = detail.floatCommission; existingVoucherDTL.floatAmount = detail.floatAmount; ctx.SaveChanges(); } id++; } int _intId = 1; var obj1 = ctx.tblVoucherDTLs.OrderByDescending(t => t.intId).FirstOrDefault(); if (obj1 != null) { _intId = obj1.intId + 1; } foreach (var detail1 in voucher.voucherDTL1) { var detailVoucher1 = new tblVoucherDTL { intId = _intId, intCategory = detail1.intCategory, intVNo = voucher.intVoucherNo, intItemCode = detail1.intItemCode, intPieces = detail1.intPieces, floatRate = detail1.floatRate, floatCommission = detail1.floatCommission, floatAmount = detail1.floatAmount }; ctx.tblVoucherDTLs.Add(detailVoucher1); _intId++; ctx.SaveChanges(); } //else //{ // return NotFound(); //} } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } return(Ok()); }