replacing codes in string with values from database
<p>I am working on replacing some codes in string. I have a function which gets an email template from emailtemplates table with some codes like <code>##NewUserFirstName##</code> and <code>##password##</code>
 in body column of that table. There is another table which has defined what value from which table will replace these codes so for <code>##NewUserFirstName##</code> I have defined <code>UserProfile.FirstName</code> and for <code>##password##</code> <code>UserProfile.password</code> in second table. I have got all merge codes like this:</p>

<pre><code>  using (IMailMergeCodeManager mailMergeCodeManager = new ManagerFactory().GetMailMergeCodeManager())
    {
        mailMergeCodes = mailMergeCodeManager.GetAll().ToList();
    }
</code></pre>

<p>Now I am replacing the codes in template with these codes like this:</p>

<pre><code> using (ICampaignManager campaignManager = new ManagerFactory().GetCampaignManager())
 {
     using (IUserProfileManager UserProfileManager1 = new ManagerFactory().GetUserProfileManager())
     {
         using (IAccountManager AccountManager = new ManagerFactory().GetAccountManager())
         {
             using (ICallTypeManager CallTypeManager = new ManagerFactory().GetCallTypeManager())
             {
                 using (ICallOutcomeManager CallOutComeManager = new ManagerFactory().GetCallOutcomeManager())
                 {
                     using (ICallRecordingManager CallRecordingManager = new ManagerFactory().GetCallRecordingManager())
                     {
                         UserProfile UserProfile = UserProfileManager1.GetByfkAccountId(Convert.ToInt32(AccountId)).FirstOrDefault();
                         Account acc = AccountManager.GetById(Convert.ToInt32(AccountId));                                      
                         UserProfile CurrentLoggedInUser = UserProfileManager1.GetByfkAccountId(Convert.ToInt32(CurrentAccount.Id)).FirstOrDefault();
                         CallRecording callRecording = CallRecordingManager.GetById(callRecordingId);

                         if (subject.Contains("##password##") || body.Contains("##password##"))
                             password = GetPassword(UserProfile.UserAuthentication.EmailAddress);


                         foreach (MailMergeCode mergeCode in mailMergeCodes)
                         {
                             if (body.Contains(mergeCode.MailMerge)) 
                                 body = Regex.Replace(body, mergeCode.MailMerge, mergeCode.FieldName, RegexOptions.IgnoreCase);


                             if (subject.Contains(mergeCode.MailMerge) &amp;&amp; !subject.Contains("##password##"))
                                 subject = Regex.Replace(subject, mergeCode.MailMerge, mergeCode.FieldName, RegexOptions.IgnoreCase);

                         }

                     }
                 }
             }
         }
     }

 }
</code></pre>

<p>}</p>

<p>But following line makes <code>##NewUserFirstName##</code> as <code>UserProfile.FirstName</code></p>

<p><code>body = Regex.Replace(body, mergeCode.MailMerge, mergeCode.FieldName, RegexOptions.IgnoreCase);</code></p>

<p>instead of getting value from <code>UserProfile.FirstName</code> which is "Joe Smith"</p>

<p>How can I solve this ?</p>
