string Register(Association association) { try { using (TransactionScope scope = new TransactionScope()) { AssociationsContext associationContext = new AssociationsContext(); var email = txtEmail.Text.ToLower(); //check id user with such email existed already var existedUser = (from u in associationContext.AssociationUsers.ObjectSet where u.AssociationId == association.Id && u.Email == email select u).SingleOrDefault(); if (existedUser != null) return "User with such email is already registered!"; //create user AssociationUser user = new AssociationUser(); user.AssociationId = association.Id; user.Country = new Country() { Id = int.Parse(ddlCountry.SelectedValue) }; ; user.Email = email; //name user.Prefix = NamePrefix.All.FirstOrDefault(p => p.Name == ddlPrefix.SelectedItem.Text); user.FirstName = txtFirstName.Text.ToUpper(); user.LastName = txtLastName.Text.ToUpper(); //phone user.OfficePhone = txtPhoneOffice.Text; user.MobilePhone = txtPhoneMobile.Text; user.HomePhone = txtPhoneHome.Text; user.IsOfficePhoneDefault = rbDefaultPhoneOffice.Checked; user.IsMobilePhoneDefault = rbDefaultPhoneMobile.Checked; user.IsHomePhoneDefault = rbDefaultPhoneHome.Checked; user.IsTravelAgency = checkTravelAgent.Checked; user.PositionTitle = txtTitle.Text.ToUpper(); user.IataNumber = txtIataNumber.Text.ToUpper(); user.CreatedTime = user.LastUpdatedTime = DateTime.UtcNow; associationContext.AttachUser(user); associationContext.SaveChanges(); //home address AssociationUserAddress homeAddress = new AssociationUserAddress(); homeAddress.Nickname = user.FullName + " HOME"; homeAddress.AssociationUserId = user.Id; homeAddress.Type = AddressType.Home; homeAddress.CountryId = user.CountryId; //work address AssociationUserAddress workAddress = new AssociationUserAddress(); workAddress.Nickname = user.FullName + " WORK"; workAddress.AssociationUserId = user.Id; workAddress.CountryId = user.CountryId; workAddress.Type = AddressType.Work; workAddress.BusinessName = txtBusinessName.Text.ToUpper(); workAddress.BuildingName = txtBuilding.Text.ToUpper(); workAddress.Address1 = txtAddress1.Text.ToUpper(); workAddress.Address2 = txtAddress2.Text.ToUpper(); if (ddlSuburb.Visible && ddlSuburb.SelectedIndex > 0) { int suburbId = int.Parse(ddlSuburb.SelectedValue); if (suburbId > 0) { workAddress.CountryId = null; workAddress.SuburbId = suburbId; } } else { if (rowState.Visible && ddlState.SelectedIndex > 0) { int stateId = int.Parse(ddlState.SelectedValue); if (stateId > 0) { workAddress.CountryId = null; workAddress.StateId = stateId; } } workAddress.SuburbName = txtSuburbName.Text.ToUpper(); workAddress.SuburbCode = txtSuburbCode.Text.ToUpper(); } associationContext.AttachUserAddress(homeAddress); associationContext.AttachUserAddress(workAddress); associationContext.SaveChanges(); AssociationUserActivation activation = new AssociationUserActivation(); activation.AssociationUserId = user.Id; activation.Guid = Guid.NewGuid(); activation.ExpiryTime = DateTime.UtcNow.AddHours(2.0); //expiry associationContext.AssociationUserActivations.Add(activation); associationContext.SaveChanges(); var emailProvider = associationContext.AssociationEmails.FirstOrDefault(e => e.AssociationId == association.Id); if (emailProvider != null) { var uri = Request.Url; string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port); string activtionLink = Path.Combine(baseUrl + Page.GetRouteUrl("activation", new { guid = activation.Guid.ToString("D") })); string contactUsLink = Path.Combine(baseUrl + Page.GetRouteUrl("contactus", null)); var txtContent = MailTemplateHelper.GetRegistratioTxtContent(association.Name.ToUpper(), user.FullName.ToUpper(), activtionLink, contactUsLink); var htmlContent = MailTemplateHelper.GetRegistratioHtmlContent(association.Name, user.FullName, activtionLink, contactUsLink); var avBody = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html); EmailHelper.SendMail(emailProvider, user.Email, association.Name.ToUpper() + " New User Registration", txtContent, null, avBody, true); } scope.Complete(); } return null; } catch (Exception ex) { StringBuilder builder = new StringBuilder(); builder.AppendLine(ex.Message); if (ex.InnerException != null) { builder.AppendLine(ex.InnerException.Message); if (ex.InnerException.InnerException != null) builder.AppendLine(ex.InnerException.InnerException.Message); } return builder.ToString(); } }