views:

49

answers:

4
        // Build email link
        confirmLink = Master.siteDomain + "/newsLetter.aspx?action=confirm&e=" + emailAddress + "&code=" + verCode;

        using (SqlCommand cmd = new SqlCommand("SELECT newRegEmailBody, newRegEmailSubj FROM tblSiteSettings WHERE (isActive = 1)", Master.cn))
        {
            SqlDataReader rdr = cmd.ExecuteReader();
            if (rdr.Read())
            {
                emailBody = rdr[0].ToString();
                emailSubj = rdr[1].ToString();
            }
            rdr.Close();
        }
        emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
        emailer.sendEmail(emailAddress, Master.noReplyEmail, emailSubj, emailBody);

It all seems to work fine, except the body is still showing up with [CONFIRMATION_LINK] in the text, any ideas?

+8  A: 

Strings are immutable. String operation generally return new string instances. Try this:

emailBody = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
codymanix
+2  A: 

The Replace method returns a new, updated string. You need to assign the results of the Replace call back to emailBody:

emailBody = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
LukeH
+2  A: 

Strings in C# are immutable, meaning they cannot be changed once they are instantiated unless explicitly pointed to another string. What happens is that you are calling "Replace" without any variable to receive the string that returns.

Change the 2nd to the last line with this code:

emailBody = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
Jon Limjap
+1  A: 

It's because Replace does not edit the emailBody variable. It returns the resultant string. In this case, you'd need:

string bodyToSend = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
emailer.sendEmail(emailAddress, Master.noReplyEmail, emailSubj, bodyToSend);
Secret Agent Man