public ActionResult UpdateDUHData(FormCollection formcollection) { var UiFCQuaterobj = formcollection.AllKeys.Where(i => i.Contains("_FC")); var UiActQuaterobj = formcollection.AllKeys.Where(i => i.Contains("_AC")); int ProjectId = Convert.ToInt32(formcollection["ProjectID"]); string projectname = string.IsNullOrEmpty(formcollection["hdnProjectName"]) ? formcollection["ProjectName"] : formcollection["hdnProjectName"]; string OpportunityName = string.IsNullOrEmpty(formcollection["hdnOpportunityName"]) ? formcollection["OpportunityName"] : formcollection["hdnOpportunityName"]; int OpportunityId = Convert.ToInt16(formcollection["OpportunityID"]); int DealStageID = 0; decimal? TCV; decimal? ACV; DateTime?BillingStratDate; string Note = string.Empty; string CreatedBy = string.Empty; int SalesForceCastID = 0; bool? IsActive = true; long? SalesForceGuid; decimal? SowStatusValue; bool? IsMapped; string OpportunityCategory = string.Empty; if (!string.IsNullOrEmpty(formcollection["OpportunityID"].ToString())) { var opportunityresult = financetoolentities.Opportunities.Where(i => i.OpportunityID == OpportunityId && i.IsUpdated == false).SingleOrDefault(); var opportunitylatestresult = financetoolentities.Opportunity_Latest .SingleOrDefault(b => b.OpportunityID == OpportunityId && b.IsUpdated == false); if (opportunityresult != null) { financetoolentities.Usp_UpdateOpportunity(opportunityresult.OpportunityID); TCV = opportunityresult.TCV; ACV = opportunityresult.acv; BillingStratDate = opportunityresult.BillingStratDate; Note = opportunityresult.Note; CreatedBy = opportunityresult.Createdby; SalesForceCastID = opportunityresult.SalesForceCastID; SalesForceGuid = opportunityresult.SalesForceGuid; SowStatusValue = opportunityresult.SowStatusValue; IsMapped = opportunityresult.IsMapped; OpportunityCategory = opportunityresult.OpportunityCategory; if (projectname != "Please Select Project" && !string.IsNullOrEmpty(OpportunityId.ToString())) { // IsActive = false; DealStageID = 4; IsMapped = true; } else { // IsActive = true; DealStageID = Convert.ToInt16(formcollection["dealstageId"]); IsMapped = false; } } else { financetoolentities.Usp_UpdateOpportunityLatest(opportunitylatestresult.OpportunityID); TCV = opportunitylatestresult.TCV; ACV = opportunitylatestresult.acv; BillingStratDate = opportunitylatestresult.BillingStratDate; Note = opportunitylatestresult.Note; CreatedBy = opportunitylatestresult.Createdby; SalesForceCastID = opportunitylatestresult.SalesForceCastID; SalesForceGuid = opportunitylatestresult.SalesForceGuid; SowStatusValue = opportunitylatestresult.SowStatusValue; IsMapped = opportunitylatestresult.IsMapped; OpportunityCategory = opportunitylatestresult.OpportunityCategory; if (projectname != "Please Select Project" && !string.IsNullOrEmpty(OpportunityId.ToString())) { //IsActive = false; DealStageID = 4; IsMapped = true; } else { // IsActive = true; DealStageID = Convert.ToInt16(formcollection["dealstageId"]); IsMapped = false; } } financetoolentities.Usp_InsertOpportunityLatest(Convert.ToInt16(formcollection["OpportunityID"]), OpportunityName, DealStageID, Convert.ToInt16(formcollection["SowStatus"]), Convert.ToInt16(formcollection["AMID"]), Convert.ToInt16(formcollection["selectedcustomerId"]), formcollection["ProgramName"], Convert.ToInt16(formcollection["ServiceLineID"]), Convert.ToInt16(formcollection["ProductGroupID"]), Convert.ToInt16(formcollection["DBBLDUID"]), TCV, ACV, Convert.ToInt16(formcollection["SowStatus"]), BillingStratDate, Note, CreatedBy, System.DateTime.Now, Session["UserName"].ToString(), System.DateTime.Now, SalesForceCastID, IsActive, SalesForceGuid, Convert.ToInt16(formcollection["DUID"]), Convert.ToInt16(formcollection["LOBID"]), SowStatusValue, Convert.ToInt16(Session["UserId"]), Convert.ToInt16(Session["RoleId"]), IsMapped, false, OpportunityCategory); financetoolentities.SaveChanges(); } var myACTQuaterList = financetoolentities.quater_Actual.Where(i => i.IsActive == true).ToList(); var myFCQuaterList = financetoolentities.Quaters.Where(i => i.IsActive == true).ToList(); foreach (var quarters in myACTQuaterList) { var actresult = financetoolentities.ProjectActualByDUHs .Where(i => i.OpportunityID == OpportunityId && i.QuaterID == quarters.QuaterID).Where(i => i.IsNew == true).SingleOrDefault(); if (actresult != null) { if (quarters.QuaterID == actresult.QuaterID && !string.IsNullOrEmpty(formcollection[quarters.QuaterName + "_AC"])) { //making old records as o actresult.IsNew = false; financetoolentities.SaveChanges(); //Added new records ProjectActualByDUH _projactobj = new ProjectActualByDUH(); _projactobj.Createdby = Session["UserName"].ToString(); _projactobj.actualvaluebyduh = string.IsNullOrEmpty(formcollection[quarters.QuaterName.ToString() + "_AC"]) == true ? 0 : Convert.ToDecimal(formcollection[quarters.QuaterName.ToString() + "_AC"]); _projactobj.OpportunityID = OpportunityId; _projactobj.Createddate = System.DateTime.Now; _projactobj.Modifiedby = Session["UserName"].ToString(); _projactobj.ModifiedDate = System.DateTime.Now; _projactobj.QuaterID = quarters.QuaterID; _projactobj.IsNew = true; financetoolentities.ProjectActualByDUHs.Add(_projactobj); financetoolentities.SaveChanges(); } } else { ProjectActualByDUH _projactobj = new ProjectActualByDUH(); _projactobj.Createdby = Session["UserName"].ToString(); _projactobj.actualvaluebyduh = string.IsNullOrEmpty(formcollection[quarters.QuaterName.ToString() + "_AC"]) == true ? 0 : Convert.ToDecimal(formcollection[quarters.QuaterName.ToString() + "_AC"]); _projactobj.OpportunityID = OpportunityId; _projactobj.Createddate = System.DateTime.Now; _projactobj.Modifiedby = Session["UserName"].ToString(); _projactobj.ModifiedDate = System.DateTime.Now; _projactobj.QuaterID = quarters.QuaterID; _projactobj.IsNew = true; financetoolentities.ProjectActualByDUHs.Add(_projactobj); financetoolentities.SaveChanges(); } } foreach (var quarters in myFCQuaterList) { var fcresult = financetoolentities.ProjectForecastByDUHs .Where(i => i.OpportunityID == OpportunityId && i.QuaterID == quarters.QuaterID).Where(i => i.IsNew == true).SingleOrDefault(); if (fcresult != null) { if (quarters.QuaterID == fcresult.QuaterID && !string.IsNullOrEmpty(formcollection[quarters.QuaterName + "_FC"].ToString())) { //Old forecast values are becoming null fcresult.IsNew = false; financetoolentities.SaveChanges(); //Adding new records ProjectForecastByDUH _projfcobj = new ProjectForecastByDUH(); _projfcobj.Createdby = Session["UserName"].ToString(); _projfcobj.forecastvaluebyduh = string.IsNullOrEmpty(formcollection[quarters.QuaterName.ToString() + "_FC"]) == true ? 0 : Convert.ToDecimal(formcollection[quarters.QuaterName.ToString() + "_FC"]); _projfcobj.OpportunityID = OpportunityId; _projfcobj.Createddate = System.DateTime.Now; _projfcobj.Modifiedby = Session["UserName"].ToString(); _projfcobj.ModifiedDate = System.DateTime.Now; _projfcobj.QuaterID = quarters.QuaterID; _projfcobj.IsNew = true; financetoolentities.ProjectForecastByDUHs.Add(_projfcobj); financetoolentities.SaveChanges(); } } else { ProjectForecastByDUH _projfcobj = new ProjectForecastByDUH(); _projfcobj.Createdby = Session["UserName"].ToString(); _projfcobj.forecastvaluebyduh = string.IsNullOrEmpty(formcollection[quarters.QuaterName.ToString() + "_FC"]) == true ? 0 : Convert.ToDecimal(formcollection[quarters.QuaterName.ToString() + "_FC"]); _projfcobj.OpportunityID = OpportunityId; _projfcobj.Createddate = System.DateTime.Now; _projfcobj.Modifiedby = Session["UserName"].ToString(); _projfcobj.ModifiedDate = System.DateTime.Now; _projfcobj.QuaterID = quarters.QuaterID; _projfcobj.IsNew = true; financetoolentities.ProjectForecastByDUHs.Add(_projfcobj); financetoolentities.SaveChanges(); } } return(RedirectToAction("Index")); }