public int DeleteCustomer(string CustomerNo) { int returnVal = 0; try { using (var dBContext = new CustomerReportContext()) { Model.Entity.TblCustomer emp = new Model.Entity.TblCustomer(); Model.Entity.TblInvoices inv = new Model.Entity.TblInvoices(); //Customer DeleteItem = new Customer(); //Add record { emp = dBContext.TblCustomer.FirstOrDefault(asd => asd.CustomerNo == CustomerNo); inv = dBContext.TblInvoices.FirstOrDefault(asd => asd.CustomerNo == CustomerNo); if (inv == null) { if (emp != null) { //emp = new Employes(); //emp.Id = EmployesModel.Id; dBContext.TblCustomer.Remove(emp); returnVal = dBContext.SaveChanges(); } } else { returnVal = -1; } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(returnVal); }
public int UpdateInvoice(Invoice InvoiceModel, string InvoiceNo, string InvoiceName) { List <Invoice> Items = new List <Invoice>(); int returnVal = 0; try { using (var dBContext = new CustomerReportContext()) { Invoice get; foreach (var it in dBContext.TblInvoices) { get = new Invoice(); get.InvoiceNo = it.InvoiceNo; get.CustomerNo = it.CustomerNo; get.InvoiceDate = it.InvoiceDate; get.InvoiceAmount = it.InvoiceAmount; get.PaymentDueDate = it.PaymentDueDate; Items.Add(get); } Model.Entity.TblInvoices Cust = new Model.Entity.TblInvoices(); //Add record Cust = dBContext.TblInvoices.FirstOrDefault(asd => asd.InvoiceNo == InvoiceModel.InvoiceNo); //emp = new Employes(); Cust.InvoiceNo = InvoiceModel.InvoiceNo; Cust.CustomerNo = InvoiceModel.CustomerNo; Cust.InvoiceDate = InvoiceModel.InvoiceDate; Cust.InvoiceAmount = InvoiceModel.InvoiceAmount; Cust.PaymentDueDate = Cust.InvoiceDate.AddDays(30); dBContext.TblInvoices.Update(Cust); InvoiceNo = Cust.InvoiceNo; bool Inv = Items.Any(asd => asd.InvoiceNo == InvoiceNo); if (Inv == true) { returnVal = -1; } if (Inv == false) { dBContext.TblInvoices.Add(Cust); returnVal = dBContext.SaveChanges(); } //CustomerNo = Cust.CustomerNo; //CustomerName = Cust.CustomerName; //bool departmentsame = Items.Any(asd => asd.CustomerName == CustomerName); //bool departmentexist = Items.Any(asd => (asd.CustomerNo == CustomerNo) && (asd.CustomerName == CustomerName)); //if (departmentexist == true) //{ // returnVal = dBContext.SaveChanges(); //} //else if (departmentsame == true) //{ // returnVal = -1; //} //else //{ // returnVal = dBContext.SaveChanges(); //} returnVal = dBContext.SaveChanges(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(returnVal); }