Table not showing and the tags are being shown

Padmanabhan, Venkatesh 120 Reputation points
2023-04-10T15:57:27.6566667+00:00

Hi. I am trying to create a html table from datatable and send the information in email. I receive the email, but the table is not getting created and i see only the tags and the data. It is not shown in html table format . Below is the code used.. How to fix this ?

private static string buildtable()
{
    if (dataResult.Rows.Count > 0)
            {
               
                string strData = "<p> <table border=" + 1 + " cellpadding=" + 0 + " cellspacing=" + 0 + " width = 100% >" +
                "<tr bgcolor='#4da6ff'><td><b> Name </b></td> <td> <b> Expected Marks</b> </td></tr>";

                foreach (DataRow row in dataResult.Rows)
                {
 strData += "<tr><td>" + row["User_NAME"] + "</td><td> " + row["OS_MARKS"] + "</td> </tr>";
                }
strData += "</table>";
}

 sendemail(strData);
}

 private static void sendemail(string data_table)
        {
            MailMessage message = new MailMessage();
            message.To.Add("email id");
            message.From = new MailAddress("email id");            
            message.Subject = "Marksheet";

            string strTest = "Hi Team, <br/>" +
                " Please verify the progress <br/>" + data_table;
 SmtpClient client = new SmtpClient();
            client.Host = "host";
            message.Body = strTest ;
            message.IsBodyHtml = true;
            client.Send(message);
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,027 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Karen Payne MVP 35,441 Reputation points
    2023-04-14T18:21:21.6033333+00:00

    Here is a basic version to create a message. You can adapt to your code and add formatting.

    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    
    namespace SampleNamespace.Classes
    {
        public class DataOperations
        {
            private static readonly string ConnectionString = 
                "Data Source=.\\sqlexpress;Initial Catalog=DataGridViewCodeSample;Integrated Security=True";
    
    
            public static string CreateMessage()
            {
                StringBuilder builder = new StringBuilder();
                var dtColor = new DataTable();
    
                using (var cn = new SqlConnection { ConnectionString = ConnectionString })
                {
                    using (var cmd = new SqlCommand { Connection = cn })
                    {
                        cn.Open();
    
                        cmd.CommandText = "SELECT ColorId,ColorText FROM Colors ORDER BY ColorText";
                        dtColor.Load(cmd.ExecuteReader());
    
                        builder.AppendLine("<P>");
    
                        builder.AppendLine("    <table>");
    
                        foreach (DataRow row in dtColor.Rows)
                        {
                            builder.AppendLine("        <tr>");
                            builder.AppendLine($"           <td>{row.Field<int>("ColorId")}</td><td>{row.Field<string>("ColorText")}</td>");
    
                            builder.AppendLine("        </tr>");
                        }
    
                        builder.AppendLine("    </table>");
                        builder.AppendLine("<P>");
                    }
                }
    
                return builder.ToString();
    
            }
        }
    }
    
    

    Output

    <P>
        <table>
            <tr  bgcolor='#4da6ff'>
               <td>7</td><td>Black</td>
            </tr>
            <tr  bgcolor='#4da6ff'>
               <td>2</td><td>Blue</td>
            </tr>
            <tr  bgcolor='#4da6ff'>
               <td>5</td><td>Brown</td>
            </tr>
            <tr  bgcolor='#4da6ff'>
               <td>6</td><td>DarkOrchid</td>
            </tr>
            <tr  bgcolor='#4da6ff'>
               <td>3</td><td>Green</td>
            </tr>
            <tr  bgcolor='#4da6ff'>
               <td>1</td><td>Red</td>
            </tr>
            <tr  bgcolor='#4da6ff'>
               <td>8</td><td>Silver</td>
            </tr>
            <tr  bgcolor='#4da6ff'>
               <td>4</td><td>White</td>
            </tr>
        </table>
    <P>
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.