protected void SaveRecord(bool newRecord) { // new record / exiting record // object oldEntity = null; object oldAddress = null; if (newRecord) { Entity = new CRM_School(); Entity.IsArchived = false; Entity.CRM_Address = new CRM_Address(); Entity.PrimaryContactReference = ""; db.CRM_Schools.InsertOnSubmit(Entity); } else { oldEntity = Entity.ShallowCopy(); oldAddress = Entity.CRM_Address.ShallowCopy(); } if (ddlLEA.SelectedValue != "") { Entity.CRM_LEAID = Convert.ToInt32(ddlLEA.SelectedValue); } else { Entity.CRM_LEAID = null; } Entity.Name = txtOrganisationName.Text; Entity.CRM_SchoolTypeID = Convert.ToInt32(ddlSchoolType.SelectedValue); Entity.ApproxPupils = Convert.ToInt32(txtPupils.Text); Entity.SageAccountNumber = txtSage.Text; Entity.SENSupportFreq = txtSENFrequency.Text; Entity.IsSEN = chkIsSEN.Checked; Entity.Email = txtEmail.Text; Entity.Phone = txtPhone.Text; ucAddress.Save(Entity.CRM_Address); db.CRM_SchoolKeystages.DeleteAllOnSubmit(Entity.CRM_SchoolKeystages); db.SubmitChanges(); foreach (ListItem item in chkKeyStages.Items) { if (item.Selected) { CRM_SchoolKeystage sKs = new CRM_SchoolKeystage() { CRM_KeyStageID = Convert.ToInt32(item.Value), CRM_SchoolID = Entity.ID }; db.CRM_SchoolKeystages.InsertOnSubmit(sKs); db.SubmitChanges(); } } if (oldEntity != null) { CRM.Code.History.History.RecordLinqUpdate(AdminUser, oldEntity, Entity); CRM.Code.History.History.RecordLinqUpdate(AdminUser, oldAddress, Entity.CRM_Address); db.SubmitChanges(); } else { CRM.Code.History.History.RecordLinqInsert(AdminUser, Entity); CRM.Code.History.History.RecordLinqInsert(AdminUser, Entity.CRM_Address); } ucCustomFields.Save(Entity.Reference); }
public void RunImport(int startLine, int endLine, string path, CRM_KeyStage stage) { MainDataContext db = new MainDataContext(); HttpServerUtility Server = HttpContext.Current.Server; using (StreamReader reader = new StreamReader(Server.MapPath(path))) { lineNumber = 0; while (!reader.EndOfStream) { lineNumber++; string contents = reader.ReadLine(); if (lineNumber >= startLine && lineNumber <= endLine) { string[] contentsSplit = contents.Split(','); string errorMessage = ""; CRM_School school = db.CRM_Schools.FirstOrDefault(s => s.Name == contentsSplit[(int)accountPos.estabilshmentName] && s.CRM_Address.Postcode.Trim().Replace(" ", "").ToLower() == contentsSplit[(int)accountPos.postcode].Trim().Replace(" ", "").ToLower()); CRM_LEA lea = db.CRM_LEAs.Single(s => s.ID == 35); // edinburgh CRM_SchoolType type = db.CRM_SchoolTypes.Single(s => s.ID == 1); // none Country country = db.Countries.Single(s => s.ID == 1); // uk if (school == null) { CRM_Address address = new CRM_Address() { AddressLine1 = contentsSplit[(int)accountPos.address1].Trim(), AddressLine2 = contentsSplit[(int)accountPos.address2].Trim(), AddressLine3 = "", AddressLine4 = "", AddressLine5 = "", Town = contentsSplit[(int)accountPos.towncity], County = "", Postcode = contentsSplit[(int)accountPos.postcode], CountryID = country.ID }; db.CRM_Addresses.InsertOnSubmit(address); db.SubmitChanges(); school = new CRM_School() { CRM_AddressID = address.ID, ApproxPupils = 0, CRM_LEAID = lea.ID, CRM_RegionID = null, CRM_SchoolTypeID = type.ID, Email = contentsSplit[(int)accountPos.email].Trim(), IsArchived = false, IsSEN = false, Name = contentsSplit[(int)accountPos.estabilshmentName].Trim(), Phone = contentsSplit[(int)accountPos.phone], SageAccountNumber = "", PrimaryContactReference = "", SENSupportFreq = "", }; db.CRM_Schools.InsertOnSubmit(school); db.SubmitChanges(); CRM_SchoolKeystage keystage = new CRM_SchoolKeystage() { CRM_SchoolID = school.ID, CRM_KeyStageID = stage.ID, }; db.CRM_SchoolKeystages.InsertOnSubmit(keystage); db.SubmitChanges(); } } } } }