protected void btnSave_Click(object sender, EventArgs e) { Ins_ChaForm chaForm = new Ins_ChaForm(); using (TheFacade facade = new TheFacade()) { if (CurrentChaFormID > 0) { chaForm = facade.InsentiveFacade.GetChaFormByID(CurrentChaFormID); } CreateChaForm(chaForm); if (CurrentChaFormID > 0) { facade.Update <Ins_ChaForm>(chaForm); } else { chaForm.Status = Convert.ToInt32(EnumCollection.ChaFormStatus.Draft); facade.Insert <Ins_ChaForm>(chaForm); } List <Ins_ChaFormInvoice> chaFormInvoices = facade.InsentiveFacade.GetchaFormInvoiceByChaFormID(chaForm.ID); //TODO: Get invoiceIds from Session["InvoiceIds"]; foreach (Ins_ChaFormInvoice item in chaFormInvoices) { facade.Delete <Ins_ChaFormInvoice>(item); } var invoiceIds = new List <long>(); //List<Ins_ChaFormInvoice> list = new List<Ins_ChaFormInvoice>(); if (Session["InvoiceIds"] != null) { var invoiceIdsFromSession = (List <long>)Session["InvoiceIds"]; invoiceIds.AddRange(invoiceIdsFromSession); } foreach (var invoiceId in invoiceIds) { var chaInvoice = new Ins_ChaFormInvoice(); chaInvoice.InvoiceMasterId = invoiceId; chaInvoice.ChaFormId = chaForm.ID; facade.Insert <Ins_ChaFormInvoice>(chaInvoice); } } lblmessage.Text = "Data save successfully"; lblmessage.Visible = true; }