Showing Table Data in Table Created Dynamically in ASP.Net C#

In ASP.NET C# we have Grid Views, Data Lists and so on for showing data but here we will create table at run time and we will show our data in that table.

The following is my SQL Server Data Table structure.

Data Table structure
                                       Image 1.

The following is the data in my table:

Data in my Table

                                      Image 2.

The following is my Aspx page is:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowData.aspx.cs" Inherits="DisplayDataInDynamicallyTable.ShowData" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title>Display Data in Dynamically Created Table</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <table style="width: 50%; text-align: center; background-color: skyblue;">  
  10.             <tr>  
  11.                 <td align="center">  
  12.                     <asp:PlaceHolder ID="DBDataPlaceHolder" runat="server"></asp:PlaceHolder>  
  13.                 </td>  
  14.             </tr>  
  15.         </table>  
  16.     </form>  
  17. </body>  
  18. </html>  
The following is my Aspx.cs code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Text;  
  10.   
  11. namespace DisplayDataInDynamicallyTable  
  12. {  
  13.     public partial class ShowData : System.Web.UI.Page  
  14.     {  
  15.         SqlDataAdapter da;  
  16.         DataSet ds = new DataSet();  
  17.         StringBuilder htmlTable = new StringBuilder();  
  18.   
  19.         protected void Page_Load(object sender, EventArgs e)  
  20.         {  
  21.             if (!Page.IsPostBack)  
  22.                 BindData();  
  23.         }  
  24.   
  25.         private void BindData()  
  26.         {  
  27.             SqlConnection con = new SqlConnection();  
  28.             con.ConnectionString = @"Data Source=MYPC\SqlServer2k8;Integrated Security=true;Initial Catalog=MyCompany";  
  29.             SqlCommand cmd = new SqlCommand("SELECT * FROM Customer", con);  
  30.             da = new SqlDataAdapter(cmd);  
  31.             da.Fill(ds);  
  32.             con.Open();  
  33.             cmd.ExecuteNonQuery();  
  34.             con.Close();  
  35.   
  36.             htmlTable.Append("<table border='1'>");  
  37.             htmlTable.Append("<tr style='background-color:green; color: White;'><th>Customer ID.</th><th>Name</th><th>Address</th><th>Contact No</th></tr>");  
  38.   
  39.             if (!object.Equals(ds.Tables[0], null))  
  40.             {  
  41.                 if (ds.Tables[0].Rows.Count > 0)  
  42.                 {  
  43.   
  44.                     for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
  45.                     {  
  46.                         htmlTable.Append("<tr style='color: White;'>");  
  47.                         htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["CustID"] + "</td>");  
  48.                         htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Name"] + "</td>");  
  49.                         htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Address"] + "</td>");  
  50.                         htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["ContactNo"] + "</td>");  
  51.                         htmlTable.Append("</tr>");  
  52.                     }  
  53.                     htmlTable.Append("</table>");  
  54.                     DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });  
  55.                 }  
  56.                 else  
  57.                 {  
  58.                     htmlTable.Append("<tr>");  
  59.                     htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");  
  60.                     htmlTable.Append("</tr>");  
  61.                 }  
  62.             }  
  63.         }  
  64.     }  
  65. }  
Now run the application; it will look as in the following:

Run The Application
                                                                     Image 3.