Ke7in
Ke7in

Reputation: 927

Datatable as mail in C# console application

I am trying to create html from my dataset using the following code

    public static string getHtml(DataTable dataSet)
    {
        try
        {
            string messageBody = "<font>The following are the records: </font><br><br>";

            if (dataSet.Rows.Count == 0)
                return messageBody;
            string htmlTableStart = "<table style=\"float:left; border-collapse:collapse; text-align:center;\" >";
            string htmlTableEnd = "</table>";
            string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
            string htmlHeaderRowEnd = "</tr>";
            string htmlTrStart = "<tr style =\"color:#555555;\">";
            string htmlTrEnd = "</tr>";
            string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
            string htmlTdEnd = "</td>";


            foreach (DataColumn dc in dataSet.Columns)
            {

                messageBody += htmlTableStart;
                messageBody += htmlHeaderRowStart;
                messageBody += htmlTdStart + dc + htmlTdEnd;
                messageBody += htmlHeaderRowEnd;

                foreach (DataRow Row in dataSet.Rows)
                {
                    messageBody = messageBody + htmlTrStart;
                    messageBody = messageBody + htmlTdStart + Row["" + dc + ""] + htmlTdEnd;
                    messageBody = messageBody + htmlTrEnd;
                }
            }
            messageBody = messageBody + htmlTableEnd;


            return messageBody;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

enter image description here

I want to render in a way that All different tables should come in horizontally. I tried float:left but when i use this html to be send in mail , float-left don't work

Can something be changed in code so that Tables come in hozizontally asenter image description here

Upvotes: 2

Views: 1744

Answers (1)

Ke7in
Ke7in

Reputation: 927

I completed my answer . here is the solution to convert dataset to html

public static string DataTableToHTML(DataTable dataSet)
        {
            mLogger.Error("DataTableToHTML -------> Start");
            try
            {
                string messageBody = "<font>Following corporate actions are not updated in Security Master: </font><br><br>";

                if (dataSet.Rows.Count == 0)
                    return messageBody;
                string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >";
                string htmlTableEnd = "</table>";
                string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
                string htmlHeaderRowEnd = "</tr>";
                string htmlTrStart = "<tr style =\"color:#555555;\">";
                string htmlTrEnd = "</tr>";
                string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
                string htmlTdEnd = "</td>";

                messageBody += htmlTableStart;
                messageBody += htmlHeaderRowStart;
                foreach (DataColumn dc in dataSet.Columns)
                {
                    messageBody += htmlTdStart + dc + htmlTdEnd;

                } messageBody += htmlHeaderRowEnd;

                foreach (DataRow dr in dataSet.Rows)
                {
                    messageBody = messageBody + htmlTrStart;
                    foreach (DataColumn dc in dataSet.Columns)
                    {

                        messageBody = messageBody + htmlTdStart + dr["" + dc + ""] + htmlTdEnd;
                    }
                    messageBody = messageBody + htmlTrEnd;
                }

                messageBody = messageBody + htmlTableEnd;

                mLogger.Error("DataTableToHTML -------> End");
                return messageBody;
            }
            catch (Exception ex)
            {
                mLogger.Error("Exception in DatatableToHTML" + ex);
                return null;
            }
        }

Upvotes: 1

Related Questions