Generating a table on the fly from a DB

Posted on 2004-11-08
Last Modified: 2010-04-15
Hi All,

I have a table with some records and I want to generate a HTML tbale to display them ... this is what I have done ...

opend database
execute SQL statement
get back recordset

StringBuilder sb = new StringBuilder()
string sValue1 = "";
string sValue2 = "";
string sValue3 = "";

   sValue1 = rs.getvalue(Col1);
   sValue2= rs.getvalue(Col2);
   sValue3 = rs.getvalue(Col3);
} (while rs.getNextRecord());



If I have 5000 records the DB call goes quick but the text generation takes ages ...

Is there a way to speed this up ?

I DO NOT want to use a datagrid, I want to generate my own ...
Question by:Pete2003
    LVL 1

    Accepted Solution

    You will find that the speed issue is not being caused by the HTML rendering but by the actual string concatenation that you are doing.

    It is the x = x+ y that is causing the speed issue.

    You can verify this by using a simple response.write for each  row and column instead of performing the concatenation.

    So instead of concatenating the whole table together and then writing it one time, if it is possible for you to use response.write for each row and TD then you will see an enormous speed increase here.

    If this is not possible, then you could always return the data as xml and apply an XSLT stylesheet to it which will be a lightning fast way to solve the issue.

    Either way you go, get rid of the concatenation and you will no longer have the problem.

    :) thanks
    LVL 5

    Expert Comment

    I also got the same problem for exporting data to excel.If you dont have the <table><tr><td> format things it will be faster with the string builder to display data.Please do let me know if you find with the table tags.

    Author Comment

    how else would you display a table without the table tags ??
    LVL 11

    Expert Comment

    instead of  sb.Append("<td>"+sValue1+"</td>");


    Author Comment

    ok sorry that was a creative type on my part up top ... but even if everything is included in Appends and there is no +
    I need to know if there is a way of making it fatser ... but I really like ferguslogic's idea of the multiple writes

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now