Question

When I'm trying to email the result that i have got those from the database.. then only first row is getting selected. I have put a break point and found out that I'm getting multiple values in my dsresult but when appending dsresult to the email then email is selecting only the first result.

I guess i should not use the dsresult. But any other which is gives me all the values in one row or which appends all the calues to the email body.

Here is the code...

        BAdmin objBAdmin = new BAdmin();
        BEAdmin objBEAdmin = new BEAdmin();

        BAdmin objSpAdmin = new BAdmin();
        objSpAdmin.BGetInvalidCourses(objBEAdmin);

        string invalid = objBEAdmin.DsResult.Tables[0].Rows[0]["CourseId"].ToString();
        MailMessage email = new MailMessage();

        StringBuilder body = new StringBuilder();


         email = new MailMessage("email@abc.com", ConfigurationManager.AppSettings["Email_To"]);
        email.Subject = "Invalid Courses ";
        email.CC.Add(ConfigurationManager.AppSettings["FYIEmail_CC_Dataload"]);
        email.Body = "Test message: please ignore." + invalid;
        body.Append(@"<tr><td>Hi,<br/><br/> Please find the missing courses </p></td></tr>");
        body.Append("</table>");
        SmtpClient client = new SmtpClient("pod51041.outlook.com", 587);
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["password"]);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = true;
        client.Send(email);
Was it helpful?

Solution

Hope this works. You forgot to add email.body.tostring();

        BAdmin objBAdmin = new BAdmin();
        BEAdmin objBEAdmin = new BEAdmin();

        BAdmin objSpAdmin = new BAdmin();
        objSpAdmin.BGetInvalidCourses(objBEAdmin);

     string invalid = objBEAdmin.DsResult.Tables[0].Rows[0]["CourseId"].ToString();
        MailMessage email = new MailMessage();

        StringBuilder body = new StringBuilder();


         email = new MailMessage("abc@abc.com", ConfigurationManager.AppSettings["FYIEmail_To_Dataload"]);
        email.Subject = "Invalid Courses ";
        email.CC.Add(ConfigurationManager.AppSettings["FYIEmail_CC_Dataload"]);
        foreach (System.Data.DataRow dr in objBEAdmin.DsResult.Tables[0].Rows)
        {
            body.Append("please ignore." + dr["CourseId"].ToString());
        }
        body.Append(@"<tr><td>Hi,<br/><br/> Please find the missing courses </p></td></tr>");
        body.Append("</table>");
        email.Body = body.ToString();
        email.IsBodyHtml = true;
        SmtpClient client = new SmtpClient("pod51041.outlook.com", 587);
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["password"]);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = true;
        client.Send(email);

OTHER TIPS

In the line

string invalid = objBEAdmin.DsResult.Tables[0].Rows[0]["CourseId"].ToString();

only the first row of the result table seems to be read. You should use something like

string invalid = String.Empty;
foreach ( DataRow iRow in abjBEAdmin.DsResult.Tables[0].Rows )
  invalid += iRow["CourseId"].ToString();

to append the CourseId column of each row to the string invalid.

Here you have written

string invalid = objBEAdmin.DsResult.Tables[0].Rows[0]["CourseId"].ToString();

So it always select the first row of the DsResult . If you need to send all the invalid Courses in the mail you should have a loop.

I've change the code...

email = new MailMessage("email@abc.com", ConfigurationManager.AppSettings["Email_To"]);
email.Subject = "Invalid Courses ";
email.CC.Add(ConfigurationManager.AppSettings["FYIEmail_CC_Dataload"]);

email.Body = "Test message:";
foreach(Datarow dr in objBEAdmin.DsResult.Tables[0].Rows)
{
body.Append("please ignore." + dr["CourseId"].ToString());
}

body.Append(@"<tr><td>Hi,<br/><br/> Please find the missing courses </p></td></tr>");
body.Append("</table>");
SmtpClient client = new SmtpClient("pod51041.outlook.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["password"]);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(email);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top