public new void Page_PreInit(object sender, EventArgs e) { base.Page_PreInit(sender, e); Entity = db.CRM_Organisations.SingleOrDefault(a => a.ID.ToString() == Request.QueryString["id"]); if (Entity == null) { NoticeManager.SetMessage("Organisation not found", "/admin/organisation/list.aspx"); } }
protected void lnkSelect_Click(object sender, EventArgs e) { CRM_Organisation organisation = db.CRM_Organisations.Single(v => v.ID.ToString() == ucACOrganisation.SelectedID); ucACOrganisation.Populate(organisation); }
public void ProcessRequest(HttpContext context) { string JSONOutput = EmptyOutput; context.Response.AddHeader("Content-type", "text/json"); using (MainDataContext db = new MainDataContext()) { string dataset = context.Request.QueryString["dataset"]; int parseddataset = Enumeration.GetEnumValueByName <JSONSet.DataSets>(dataset); string searchCriteria = HttpUtility.HtmlDecode(context.Request.QueryString["query"]).ToLower(); IEnumerable <JSONSet> jsonItems = Enumerable.Empty <JSONSet>(); switch (parseddataset) { case (int)JSONSet.DataSets.venue: { var venues = CRM_Venue.BaseSet(db); jsonItems = from p in venues where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.Name + " : " + p.CRM_Address.FormattedAddressBySep(", "), p.CRM_Address.Postcode, p.Reference.ToString(), "", p.Reference); } break; case (int)JSONSet.DataSets.person: { var persons = CRM_Person.BaseSet(db); jsonItems = from p in persons where !p.IsArchived where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.Fullname + " : " + p.PrimaryAddressRecord.FormattedAddressBySep(", "), p.DateOfBirthOutput, p.ID.ToString(), p.Photo, p.Reference); } break; case (int)JSONSet.DataSets.archivedperson: { var persons = db.CRM_Persons; jsonItems = from p in (from p in persons where p.IsArchived select p).ToList() where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.Fullname + " : " + p.PrimaryAddressRecord.FormattedAddressBySep(", "), p.DateOfBirthOutput, p.Reference.ToString(), p.Photo, p.Reference); } break; case (int)JSONSet.DataSets.contact: { var unarchivedContacts = from p in CRM.Code.Utils.SharedObject.SharedObject.GetSharedObjects <IContact>(db) where !p.IsArchived select p; jsonItems = from p in unarchivedContacts where p.Tokens.Any(t => t.ToLower().Contains(searchCriteria.ToLower())) orderby p.Lastname, p.Firstname select new JSONSet(p.Fullname + " : " + p.PrimaryAddress.FormattedAddressBySep(", "), p.OutputTableName, p.Reference, p.Photo, p.CRM_PersonReference); } break; case (int)JSONSet.DataSets.organisation: { var orgs = CRM_Organisation.BaseSet(db); jsonItems = from p in orgs where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.Name + " : " + p.CRM_Address.FormattedAddressBySep(", "), p.CRM_OrganisationType.Name, p.ID.ToString(), "", p.Reference); } break; case (int)JSONSet.DataSets.school: { var schools = CRM_School.BaseSet(db); jsonItems = from p in schools where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.Name + " : " + p.CRM_Address.FormattedAddressBySep(", "), p.CRM_SchoolType.Name, p.ID.ToString(), "", p.Reference); } break; case (int)JSONSet.DataSets.schoolperson: { var schoolsp = CRM_PersonSchool.BaseSet(db); jsonItems = from p in schoolsp where !p.IsArchived where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.Name + " : " + p.PrimaryAddress.FormattedAddressBySep(", "), p.CRM_School.Name, p.ID.ToString(), p.CRM_Person.Photo, p.Reference); } break; case (int)JSONSet.DataSets.orgperson: { var orgpers = CRM_PersonOrganisation.BaseSet(db); jsonItems = from p in orgpers where !p.IsArchived where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.Name + " : " + p.PrimaryAddress.FormattedAddressBySep(", "), p.CRM_Organisation.Name, p.ID.ToString(), p.CRM_Person.Photo, p.Reference); } break; case (int)JSONSet.DataSets.family: { var families = CRM_Family.BaseSet(db); jsonItems = from p in families where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.Name + " : " + p.CRM_FamilyPersons.Count() + " members", p.MemberList, p.ID.ToString(), "", p.Reference); } break; case (int)JSONSet.DataSets.mytasks: { var tasks = db.CRM_Tasks.ToArray().Where(c => c.IsVisible(Convert.ToInt32(HttpContext.Current.Request.QueryString["adminuserid"]))); jsonItems = from p in tasks where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.Name + "", p.DueDate.ToString("dd/MM/yyyy"), p.ID.ToString(), "", p.Reference); } break; case (int)JSONSet.DataSets.admin: { var admins = db.Admins.ToArray(); jsonItems = from p in admins where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.DisplayName + "", p.LastLogin == null ? "Never logged in" : "Last Login: "******"dd/MM/yyyy HH:mm"), p.ID.ToString(), "", p.ID.ToString()); } break; case (int)JSONSet.DataSets.schoolorgs: { var schoolorgs = CRM.Code.Utils.SharedObject.SharedObject.GetSharedObjects <ISchoolOrganisation>(db); jsonItems = from p in schoolorgs where p.Tokens.Any(t => t.Contains(searchCriteria)) select new JSONSet(p.Name + " : " + p.CRM_Address.FormattedAddressBySep(", "), p.OutputTableName, p.Reference, "", p.Reference.ToString()); } break; } if (jsonItems.Any()) { JSONOutput = JSONSet.ConvertToJSON(jsonItems); } else { jsonItems = Enumerable.Concat(jsonItems, new [] { new JSONSet("No results found", "Please alter your search", "", "", "") }); JSONOutput = JSONSet.ConvertToJSON(jsonItems); } context.Response.Write(JSONOutput); } context.Response.End(); }
public void RunImport(int startLine, int endLine) { MainDataContext db = new MainDataContext(); HttpServerUtility Server = HttpContext.Current.Server; string path = "/TaskScript/FinalListforCoalFace.csv"; 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_FormFieldItem ffItem = db.CRM_FormFieldItems.FirstOrDefault(f => f.Label == contentsSplit[(int)accountPos.Constituent]); if (ffItem == null) { ffItem = new CRM_FormFieldItem() { Label = contentsSplit[(int)accountPos.Constituent], CRM_FormFieldID = 1, OrderNo = Code.Utils.Ordering.Ordering.GetNextOrderID(db.CRM_FormFieldItems), IsActive = true, IsArchived = false }; db.CRM_FormFieldItems.InsertOnSubmit(ffItem); db.SubmitChanges(); } CRM_Person person = db.CRM_Persons.FirstOrDefault(f => f.LegacyID != null && f.LegacyID.ToString() == contentsSplit[(int)accountPos.LegacyID]); if (person == null) { CRM_Address address = new CRM_Address(); address.AddressLine1 = contentsSplit[(int)accountPos.Address1]; address.AddressLine2 = contentsSplit[(int)accountPos.Address2]; address.AddressLine3 = contentsSplit[(int)accountPos.Address3]; address.AddressLine4 = contentsSplit[(int)accountPos.Address4]; address.AddressLine5 = contentsSplit[(int)accountPos.Address5]; address.Town = contentsSplit[(int)accountPos.City]; address.County = contentsSplit[(int)accountPos.County]; address.Postcode = contentsSplit[(int)accountPos.Postcode]; address.CountryID = 1; db.CRM_Addresses.InsertOnSubmit(address); db.SubmitChanges(); person = new CRM_Person(); db.CRM_Persons.InsertOnSubmit(person); if (contentsSplit[(int)accountPos.AddressType] == "Business") { person.AddressType = (byte)CRM_Address.Types.Business; } person.AddressType = (byte)CRM_Address.Types.Home; person.DateAdded = UKTime.Now; person.DateModified = UKTime.Now; person.IsArchived = false; person.IsChild = false; person.IsCarerMinder = false; person.PreviousNames = ""; person.PrimaryEmail = ""; person.PrimaryTelephone = ""; person.Telephone2 = ""; person.IsDoNotMail = false; person.LegacyID = Convert.ToInt32(contentsSplit[(int)accountPos.LegacyID]); person.CRM_AddressID = address.ID; person.Password = ""; person.TempCode = ""; } person.Title = contentsSplit[(int)accountPos.Title]; person.Firstname = contentsSplit[(int)accountPos.Firstname]; person.Lastname = contentsSplit[(int)accountPos.Surname]; if (contentsSplit[(int)accountPos.Gender] == "Male") { person.IsMale = true; } else if (contentsSplit[(int)accountPos.Gender] == "Female") { person.IsMale = false; } else { person.IsMale = null; } if (contentsSplit[(int)accountPos.NoEmail] == "Yes") { person.IsDoNotEmail = true; } db.SubmitChanges(); CRM_FormFieldAnswer answer = new CRM_FormFieldAnswer() { Answer = contentsSplit[(int)accountPos.Constituent], CRM_FormFieldID = ffItem.CRM_FormFieldID, TargetReference = person.Reference }; db.CRM_FormFieldAnswers.InsertOnSubmit(answer); db.SubmitChanges(); if (contentsSplit[(int)accountPos.PhoneNumber11].Contains("@")) { person.PrimaryEmail = contentsSplit[(int)accountPos.PhoneNumber11]; } else { person.PrimaryTelephone = contentsSplit[(int)accountPos.PhoneNumber11]; person.PrimaryEmail = contentsSplit[(int)accountPos.PhoneNumber12]; } if (!contentsSplit[(int)accountPos.PhoneNumber12].Contains("@")) { person.Telephone2 = contentsSplit[(int)accountPos.PhoneNumber12]; } db.SubmitChanges(); if (!String.IsNullOrEmpty(contentsSplit[(int)accountPos.MembershipID].Trim())) { CRM_AnnualPassCard card = new CRM_AnnualPassCard() { MembershipNumber = Convert.ToInt32(contentsSplit[(int)accountPos.MembershipID]) }; db.CRM_AnnualPassCards.InsertOnSubmit(card); db.SubmitChanges(); CRM_AnnualPass pass = new CRM_AnnualPass(); pass.CRM_AnnualPassCardID = card.ID; pass.AmountPaid = 0; pass.CRM_AnnualPassTypeID = 15; pass.IsArchived = false; pass.DiscountApplied = ""; pass.PrimaryContactReference = person.Reference; try { if (!String.IsNullOrEmpty(contentsSplit[(int)accountPos.MembershipExpiry]) && String.IsNullOrEmpty((contentsSplit[(int)accountPos.MembershipLastRenew]))) { pass.StartDate = Code.Utils.Text.Text.FormatInputDate(contentsSplit[(int)accountPos.MembershipExpiry]); } else { pass.StartDate = Code.Utils.Text.Text.FormatInputDate(contentsSplit[(int)accountPos.MembershipExpiry]).AddYears(-1); } } catch { pass.StartDate = UKTime.Now; } try { pass.ExpiryDate = Code.Utils.Text.Text.FormatInputDate(contentsSplit[(int)accountPos.MembershipExpiry]); } catch { pass.ExpiryDate = new DateTime(2075, 12, 31); } db.CRM_AnnualPasses.InsertOnSubmit(pass); db.SubmitChanges(); CRM_AnnualPassPerson passPerson = new CRM_AnnualPassPerson() { CRM_AnnualPassID = pass.ID, CRM_PersonID = person.ID, IsArchived = false }; db.CRM_AnnualPassPersons.InsertOnSubmit(passPerson); db.SubmitChanges(); } if (!String.IsNullOrEmpty(contentsSplit[(int)accountPos.OrgName])) { CRM_Organisation org = db.CRM_Organisations.FirstOrDefault(f => f.Name == contentsSplit[(int)accountPos.OrgName]); if (org == null) { CRM_Address orgAddress = new CRM_Address() { AddressLine1 = contentsSplit[(int)accountPos.OrgAddr1], AddressLine2 = contentsSplit[(int)accountPos.OrgAddr2], AddressLine3 = contentsSplit[(int)accountPos.OrgAddr3], AddressLine4 = contentsSplit[(int)accountPos.OrgAddr4], AddressLine5 = "", County = "", Town = contentsSplit[(int)accountPos.OrgAddrCity], Postcode = contentsSplit[(int)accountPos.Postcode], CountryID = 1 }; db.CRM_Addresses.InsertOnSubmit(orgAddress); db.SubmitChanges(); org = new CRM_Organisation() { CRM_OrganisationTypeID = 1, CRM_AddressID = orgAddress.ID, Name = contentsSplit[(int)accountPos.OrgName], IsArchived = false, PrimaryContactReference = person.Reference }; db.CRM_Organisations.InsertOnSubmit(org); db.SubmitChanges(); } CRM_Role role = db.CRM_Roles.FirstOrDefault(f => f.Name == contentsSplit[(int)accountPos.OrgPosition]); if (role == null) { role = new CRM_Role(); db.CRM_Roles.InsertOnSubmit(role); role.Name = contentsSplit[(int)accountPos.OrgPosition]; db.SubmitChanges(); } CRM_PersonOrganisation personOrg = new CRM_PersonOrganisation() { CRM_OrganisationID = org.ID, CRM_RoleID = role.ID, IsArchived = false, Email = "", Telephone = "", CRM_PersonID = person.ID }; db.CRM_PersonOrganisations.InsertOnSubmit(personOrg); db.SubmitChanges(); } } } } }
protected void RunImport(int startLine, int endLine) { MainDataContext db = new MainDataContext(); HttpServerUtility Server = HttpContext.Current.Server; string path = "/TaskScript/Art_Booking.csv"; 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_Person person = db.CRM_Persons.FirstOrDefault(f => f.PrimaryEmail.Trim().ToLower() == contentsSplit[(int)accountPos.Email].Trim().ToLower() && f.Lastname.Trim().ToLower() == contentsSplit[(int)accountPos.Lastname].Trim().ToLower()); if (person == null) { CRM_Address address = new CRM_Address(); address.AddressLine1 = contentsSplit[(int)accountPos.Address1]; address.AddressLine2 = contentsSplit[(int)accountPos.Address2]; address.AddressLine3 = contentsSplit[(int)accountPos.Address3]; address.AddressLine4 = contentsSplit[(int)accountPos.Address4]; address.AddressLine5 = ""; address.Town = ""; address.County = contentsSplit[(int)accountPos.Address4]; address.Postcode = contentsSplit[(int)accountPos.Postcode]; address.CountryID = 1; db.CRM_Addresses.InsertOnSubmit(address); db.SubmitChanges(); person = new CRM_Person(); db.CRM_Persons.InsertOnSubmit(person); person.AddressType = (byte)CRM_Address.Types.Home; person.DateAdded = UKTime.Now; person.IsArchived = false; person.IsChild = false; person.IsCarerMinder = false; person.PreviousNames = ""; person.PrimaryEmail = contentsSplit[(int)accountPos.Email]; person.PrimaryTelephone = contentsSplit[(int)accountPos.Phone1]; person.Telephone2 = contentsSplit[(int)accountPos.Phone2]; person.IsDoNotMail = false; person.LegacyID = null; person.CRM_AddressID = address.ID; person.Password = ""; person.TempCode = ""; } person.DateModified = UKTime.Now; person.Title = ""; person.Firstname = contentsSplit[(int)accountPos.Firstname]; person.Lastname = contentsSplit[(int)accountPos.Lastname]; person.IsMale = null; person.IsDoNotEmail = false; db.SubmitChanges(); if (!String.IsNullOrEmpty(contentsSplit[(int)accountPos.Company])) { CRM_Organisation org = db.CRM_Organisations.FirstOrDefault(f => f.Name.Trim().ToLower() == contentsSplit[(int)accountPos.Company].Trim().ToLower()); if (org == null) { CRM_Address orgAddress = new CRM_Address() { AddressLine1 = contentsSplit[(int)accountPos.Address1], AddressLine2 = contentsSplit[(int)accountPos.Address2], AddressLine3 = contentsSplit[(int)accountPos.Address3], AddressLine4 = contentsSplit[(int)accountPos.Address4], AddressLine5 = "", County = "", Town = "", Postcode = contentsSplit[(int)accountPos.Postcode], CountryID = 1 }; db.CRM_Addresses.InsertOnSubmit(orgAddress); db.SubmitChanges(); org = new CRM_Organisation() { CRM_OrganisationTypeID = 1, CRM_AddressID = orgAddress.ID, Name = contentsSplit[(int)accountPos.Company], IsArchived = false, PrimaryContactReference = person.Reference, }; db.CRM_Organisations.InsertOnSubmit(org); db.SubmitChanges(); } CRM_Role role = db.CRM_Roles.FirstOrDefault(f => f.Name == ""); CRM_PersonOrganisation personOrg = new CRM_PersonOrganisation() { CRM_OrganisationID = org.ID, CRM_RoleID = role.ID, IsArchived = false, Email = "", Telephone = "", CRM_PersonID = person.ID }; db.CRM_PersonOrganisations.InsertOnSubmit(personOrg); db.SubmitChanges(); } //constituent type CRM_FormFieldAnswer consTypeAnswer = db.CRM_FormFieldAnswers.FirstOrDefault(f => f.TargetReference == person.Reference && f.CRM_FormFieldID == 1); //origin CRM_FormFieldAnswer originAnswer = db.CRM_FormFieldAnswers.FirstOrDefault(f => f.TargetReference == person.Reference && f.CRM_FormFieldID == 2); if (consTypeAnswer == null) { consTypeAnswer = new CRM_FormFieldAnswer() { Answer = "Group Booking", CRM_FormFieldID = 1, TargetReference = person.Reference }; db.CRM_FormFieldAnswers.InsertOnSubmit(consTypeAnswer); db.SubmitChanges(); } else { string[] items = consTypeAnswer.Answer.Split(new string[] { "<br/>" }, StringSplitOptions.None); if (!items.Any(i => i == "Group Booking")) { consTypeAnswer.Answer += "<br/>Group Booking"; } db.SubmitChanges(); } if (originAnswer == null) { originAnswer = new CRM_FormFieldAnswer() { Answer = "Import", CRM_FormFieldID = 2, TargetReference = person.Reference }; db.CRM_FormFieldAnswers.InsertOnSubmit(originAnswer); db.SubmitChanges(); } else { string[] items = originAnswer.Answer.Split(new string[] { "<br/>" }, StringSplitOptions.None); if (!items.Any(i => i == "Import")) { consTypeAnswer.Answer += "<br/>Import"; } db.SubmitChanges(); } } } } }
public void RunImport(int startLine, int endLine) { MainDataContext db = new MainDataContext(); HttpServerUtility Server = HttpContext.Current.Server; using (StreamReader reader = new StreamReader(Server.MapPath("/taskscript/CBM_Europe_import.csv"), Encoding.GetEncoding("iso-8859-1"))) { lineNumber = 0; while (!reader.EndOfStream) { lineNumber++; string contents = reader.ReadLine(); if (lineNumber >= startLine && lineNumber <= endLine) { string[] contentsSplit = contents.Split(','); string errorMessage = ""; CRM_Person person = db.CRM_Persons.FirstOrDefault(f => f.Firstname.ToUpper().Trim() == contentsSplit[(int)accountPos.first].ToUpper().Trim() && f.Lastname.ToUpper().Trim() == contentsSplit[(int)accountPos.last].ToUpper().Trim() && (f.PrimaryEmail.ToUpper().Trim() == contentsSplit[(int)accountPos.email].ToUpper().Trim() || contentsSplit[(int)accountPos.email] == "")); Country country = db.Countries.FirstOrDefault(f => f.Name == contentsSplit[(int)accountPos.country]); if (country == null) { country = db.Countries.Single(s => s.ID == 1); HttpContext.Current.Response.Write("country missing - " + contentsSplit[(int)accountPos.country]); } if (person == null) { HttpContext.Current.Response.Write("adding person - " + contentsSplit[(int)accountPos.last] + "<br/>"); CRM_Address address = new CRM_Address() { AddressLine1 = "", AddressLine2 = "", AddressLine3 = "", AddressLine4 = "", AddressLine5 = "", CountryID = country.ID, Postcode = "", Town = "", County = "" }; if (contentsSplit[(int)accountPos.company].Trim() == "") { address.AddressLine1 = contentsSplit[(int)accountPos.address].Replace("@", ","); address.Postcode = contentsSplit[(int)accountPos.zip].Replace("@", ","); address.Town = contentsSplit[(int)accountPos.city].Replace("@", ","); address.County = contentsSplit[(int)accountPos.state].Replace("@", ","); } db.CRM_Addresses.InsertOnSubmit(address); db.SubmitChanges(); person = new CRM_Person() { CRM_AddressID = address.ID, AddressType = (byte)CRM_Address.Types.Business, DateAdded = UKTime.Now, DateModified = UKTime.Now, DateOfBirth = null, Firstname = contentsSplit[(int)accountPos.first], Lastname = contentsSplit[(int)accountPos.last], IsArchived = false, IsCarerMinder = false, IsChild = false, IsConcession = false, IsContactEmail = true, IsContactPost = true, IsDeceased = false, IsDoNotEmail = false, IsDoNotMail = false, IsGiftAid = false, IsMale = null, LegacyID = null, PreviousNames = "", PrimaryEmail = contentsSplit[(int)accountPos.company].Trim() == "" ? contentsSplit[(int)accountPos.email] : "", PrimaryTelephone = "", Telephone2 = "", Title = "", Password = "", TempCode = "" }; db.CRM_Persons.InsertOnSubmit(person); db.SubmitChanges(); } else { HttpContext.Current.Response.Write("person exists already - " + contentsSplit[(int)accountPos.last] + "<br/>"); } CRM_FormFieldItem gallery = db.CRM_FormFieldItems.Single(s => s.ID == 9); CRM_FormFieldItem collector = db.CRM_FormFieldItems.Single(s => s.ID == 31); CRM_FormFieldResponse galleryresponse = db.CRM_FormFieldResponses.FirstOrDefault(f => f.CRM_FormFieldItemID == gallery.ID && f.TargetReference == person.Reference); CRM_FormFieldResponse collectorresponse = db.CRM_FormFieldResponses.FirstOrDefault(f => f.CRM_FormFieldItemID == collector.ID && f.TargetReference == person.Reference); if (galleryresponse == null) { HttpContext.Current.Response.Write("adding gallery for - " + contentsSplit[(int)accountPos.last] + "<br/>"); galleryresponse = new CRM_FormFieldResponse() { CRM_FormFieldItemID = gallery.ID, CRM_FormFieldID = gallery.CRM_FormFieldID, TargetReference = person.Reference, Answer = "" }; db.CRM_FormFieldResponses.InsertOnSubmit(galleryresponse); db.SubmitChanges(); } if (collectorresponse == null) { HttpContext.Current.Response.Write("adding collector for - " + contentsSplit[(int)accountPos.last] + "<br/>"); collectorresponse = new CRM_FormFieldResponse() { CRM_FormFieldItemID = collector.ID, CRM_FormFieldID = collector.CRM_FormFieldID, TargetReference = person.Reference, Answer = "" }; db.CRM_FormFieldResponses.InsertOnSubmit(collectorresponse); db.SubmitChanges(); } string companystr = contentsSplit[(int)accountPos.company].Replace("@", ",").Trim(); if (companystr != "") { CRM_Organisation company = db.CRM_Organisations.FirstOrDefault(s => s.Name.ToUpper() == companystr.ToUpper()); if (company == null) { HttpContext.Current.Response.Write("adding company for - " + contentsSplit[(int)accountPos.last] + "<br/>"); CRM_Address address = new CRM_Address() { AddressLine1 = contentsSplit[(int)accountPos.address].Replace("@", ",").Trim(), AddressLine2 = "", AddressLine3 = "", AddressLine4 = "", AddressLine5 = "", Town = contentsSplit[(int)accountPos.city].Replace("@", ","), County = "", Postcode = contentsSplit[(int)accountPos.zip].Replace("@", ","), CountryID = country.ID }; db.CRM_Addresses.InsertOnSubmit(address); db.SubmitChanges(); company = new CRM_Organisation() { CRM_AddressID = address.ID, IsArchived = false, Name = companystr, PrimaryContactReference = "", CRM_OrganisationTypeID = 1 }; db.CRM_Organisations.InsertOnSubmit(company); db.SubmitChanges(); } else { HttpContext.Current.Response.Write("company already exists for - " + contentsSplit[(int)accountPos.last] + "<br/>"); } CRM_PersonOrganisation personOrg = db.CRM_PersonOrganisations.FirstOrDefault(f => f.CRM_OrganisationID == company.ID && f.CRM_PersonID == person.ID); if (personOrg == null) { HttpContext.Current.Response.Write("adding organisation link for - " + contentsSplit[(int)accountPos.last] + "<br/>"); personOrg = new CRM_PersonOrganisation() { CRM_OrganisationID = company.ID, CRM_PersonID = person.ID, IsArchived = false, Telephone = "", Email = contentsSplit[(int)accountPos.email], CRM_RoleID = 7 // empty }; db.CRM_PersonOrganisations.InsertOnSubmit(personOrg); db.SubmitChanges(); person.PrimaryAddressID = company.CRM_AddressID; db.SubmitChanges(); } else { HttpContext.Current.Response.Write("organisation link already exists for - " + contentsSplit[(int)accountPos.last] + "<br/>"); } } else { HttpContext.Current.Response.Write("no company for - " + contentsSplit[(int)accountPos.last] + "<br/>"); } } } } }