0

I have a function in C# like below:

public static string ConvertDataTableToHTMLExtra(DataTable dt)
    {
        string html = "";
            html += "<table>";

            //add header row
            html += "<thead>";
            html += "<tr>";
            for (int i = 0; i < dt.Columns.Count; i++)
                html += "<td>" + dt.Columns[i].ColumnName + "</td>";
            html += "</tr>";
            html += "</thead>";

            //add rows
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                html += "<tr>";
                for (int j = 0; j < dt.Columns.Count; j++)
                    html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
                html += "</tr>";
            }

            html += "</table>";

        return html;
    }

The datatable is filled by using a Stored Procedure. After the content is set to dt, this function above is called. It is working a little slow but without error, as long as we have less than ~6000 rows. Above ~6000, it is returning timeout error. Execution time of SP is less than 2 seconds still.

Is there any better way to re-develop such a function? Any help or advice would be appreciated.

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
  • 5
    For starters, I'd suggest using `StringBuilder` instead of string concatenation in a loop. – Ousmane D. Jan 02 '19 at 22:38
  • Possible duplicate of [Most efficient way to concatenate strings?](https://stackoverflow.com/questions/21078/most-efficient-way-to-concatenate-strings) – Peter B Jan 02 '19 at 22:57
  • What sort of timeout error are you getting? Is your code timing out filling the datatable or generating the html? – Peter Jan 02 '19 at 23:00
  • From a UI/UX perspective > 6000 rows is excessive in *most* instances. That's a lot of information for a set of human eyeballs. – Jon P Jan 02 '19 at 23:33

1 Answers1

2

Is there any better way to re-develop such a function? Any help or advice would be appreciated.

Yes, I'd recommend that you start refactoring the code to use StringBuilder instead of String as string concatenation in a loop is rather costly.

Using the StringBuilder Class in .NET:

The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.

So, the method would look like:

static string ConvertDataTableToHTMLExtra(DataTable dt) 
{
       StringBuilder html = new StringBuilder();
       html.Append("<table>");

       //add header row
       html.Append("<thead>");
       html.Append("<tr>");

       for (int i = 0; i < dt.Columns.Count; i++)
            html.Append("<td>").Append(dt.Columns[i].ColumnName).Append("</td>");

         html.Append("</tr>");
         html.Append("</thead>");

            //add rows
       for (int i = 0; i < dt.Rows.Count; i++)
       {
             html.Append("<tr>");
             for (int j = 0; j < dt.Columns.Count; j++)
                  html.Append("<td>").Append(dt.Rows[i][j]).Append("</td>");
             html.Append("</tr>");
       }

       html.Append("</table>");

       return html.ToString();
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126