ASP.NET Stringbuilder and DataTable

Posted by in .NET Framework category on for Beginner level | Points: 50 | Views : 38084 red flag
Rating: 4 out of 5  
 3 vote(s)

N.B, This article is for newbies. I remember my early days of my programming,The most battle i used to have was how to display my data on my form, yes i used GridViews, but find limitations when dealing with data.

ASP.NET Stringbuilder and DataTable


What you need is a DataTable (PLEASE CREATE YOUR OWN DATATABLE), create stringBuilder variable, Literal on your form. You will need to use System.Text namespace in order to work with StringBuilder class.

public partial class _Default : System.Web.UI.Page
  protected void Page_Load(object sender, EventArgs e)
Write below method inside the class that will be called from the Page Load method of the code behind.
//load dataTable, and dynamically create html table, while iterating the dataTable.
        private void HowToDisplayData()
            //Create Stringbuilder, so we can append the iterated results from our DataTable.
            StringBuilder tableOutput = new StringBuilder("<table class='tableStyle'");
            tableOutput.Append("<tr><td rowspan ='2'><b>DYNAMIC TABLE.</b></td></tr>");
            tableOutput.Append("<tr></tr><tr><td>First Name: </td><td>LastName</td></tr>");
            DataTable _dtTable = new DataTable();

            //load DataTable from YOUR Data Access Layer.
            _dtTable = DALperson.LoadPersonInfor();
                if (_dtTable.Rows.Count > 0)
                    foreach (DataRow dr in _dtTable.Rows)
                       tableOutput.Append("<tr><td>"+dr["firstname"].ToString()+"</td><td>" + dr["lastname"].ToString() + "</td></tr>");
                    TxtErr.Text = "Person Infor. not found!";
                tempHtmlTable.Text = tableOutput.ToString();
            catch (Exception ex)
                _dtTable = null;
The above method will write the table output to tempHtmlTable literal control. The code for my aspx page goes here.


    <form id="form1" runat="server">
       <asp:Literal ID="tempHtmlTable" runat="server"></asp:Literal>
        <asp:Label ID="TxtErr" runat="server"></asp:Label>

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: nkasieng Safi malete
Member Level: Starter
Member Status: Member
Member Since: 7/17/2009 11:10:56 PM
Country: South Africa

Login to vote for this post.

Comments or Responses

Posted by: Ray.chayan on: 9/10/2010 | Points: 10
Hi, thanks for this nice article.
But in this case there is a for loop that acts with the each row of the datatable. So in case of large datatable this process will be effective than binding data to the datagrid, datalist, gridview or repeater. Please help me..
Posted by: Nkaiseng on: 9/14/2010 | Points: 10
Hi, how big is your DataTable? what are you trying to archive?
N.B What i like about this is, You have full control of your generated html table.
Posted by: Vuyiswamb on: 10/29/2010 | Points: 10
Good Article Safi

Posted by: Sahoomanoj27 on: 9/6/2012 | Points: 25
Excellent Article Man. This is the thing i was wondering 4.
Once Again Excellent.

Login to post response

Comment using Facebook(Author doesn't get notification)