Kartikeya
Kartikeya

Reputation: 53

How to replace HTML Contents?

I am new to C# programming. As a study I am making an application that will send an email to employees whose birthday falls on a day.
I am able to achieve the task of sending email. however my email is html enabled where I want to display the name of Birthday employee.
On HTML page, I have created a table where there will be a row for name.

<tr>
<td> Name</td>
<td>#Name#</td>
</tr>

Records I am extracting from database and storing them in data adapter and then in a string. I am using String Replace() function to update the name.

DataTable dt = null;
string Body = System.IO.File.ReadAllText("path of HTML file");
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand("query"))
    {
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@Day", DateTime.Today.Day);
        cmd.Parameters.AddWithValue("@Month", DateTime.Today.Month);
        using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
        {
            sda.Fill(dt);
        }
    }
}
foreach (DataRow row in dt.Rows)
{
    string name = row["Name"].ToString();
    string dob = row["DOB"].ToString();

    DateTime dob1 = DateTime.Parse(row["DOB"].ToString());
    if ((dob1.Day == DateTime.Today.Day) && (dob1.Month == DateTime.Today.Month))
    {
        Body = Body.Replace("#Name#", name);
        Body = Body.Replace("#dob#", dob);

        using (MailMessage mm = new MailMessage("From Email address", "To email address"))
        {
            mm.Subject = "Birthday Greetings";
            mm.Body = Body;
            mm.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "Mail Server";
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = credentials;
            smtp.Port = 587;
            smtp.Send(mm);
        }
    }
}

Replace function is being executed under a foreach loop to check for all the employees.
However, if more than one employee birthday falls together I get same name in all emails. I guess replace function works for first time and doesn't replace in the next iteration.

Request if some efficient method be provided.

Upvotes: 1

Views: 1140

Answers (3)

Gaurang Dave
Gaurang Dave

Reputation: 4046

Read HTML template file from location in your loop each time. What you need is html text with #NAME# etc. I think you are reading it one time and them running loop. In each Row you access in loop, use fresh HTML template.

Upvotes: 2

Sunil
Sunil

Reputation: 3424

First loop your string has #Name.

Second loop, now the string that is stored in Body doesn't have #Name anymore, so it doesn't get updated any more.

You need to reinitialized the Body variable from the template in the loop.

Upvotes: 1

Luke Villanueva
Luke Villanueva

Reputation: 2070

If my guess is correct I think it is because on the first execution of your loop. The whole body string is being replaced and being assign to the body variable hence your "#Name#" and "#dob#" cannot be found on the next execution. You can read the body string every loop so you have the original string each iteration to replace with.

Upvotes: 0

Related Questions