title: Display DataTable in HTML Table in ASP.Net C# Webform
body: <p>I refer this website to try to write code - Display DataTable in HTML Table in ASP.Net C# Webform：<a href="http://www.aspsnippets.com/Articles/Display-DataTable-in-HTML-Table-in-ASPNet-using-C-and-VBNet.aspx" rel="nofollow">http://www.aspsnippets.com/Articles/Display-DataTable-in-HTML-Table-in-ASPNet-using-C-and-VBNet.aspx</a></p>
  
  <p>But can't show table information,show empty page.
  How can I fix this code？Thanks.</p>
  
  <p>1.CS.aspx</p>
  
  <pre><code> &lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;style type="text/css"&gt;
  `body { font-family: Arial;font-size: 10pt; }`
  
          table {
              border: 1px solid #ccc;
              border-collapse: collapse;
          }
  
              table th {
                  background-color: #F7F7F7;
                  color: #333;
                  font-weight: bold;
              }
  
              table th, table td {
                  padding: 5px;
                  border-color: #ccc;
              }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div style="margin-left: auto; margin-right: auto; text-align: center;"&gt;
  
              &lt;asp:PlaceHolder ID="placeholder" runat="server" /&gt;
  
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>2.CS.aspx.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  using System.Data;
  using System.Text;
  using System.Configuration;
  
  
  
  
  public partial class CS : System.Web.UI.Page
  {
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!this.IsPostBack)
          {
              PlaceHolder placeholder = new PlaceHolder();
  
              //Populating a DataTable from database.
              DataTable dt = this.GetData();
  
              //Building an HTML string.
              StringBuilder html = new StringBuilder();
  
              //Table start.
              html.Append("&lt;table border = '1'&gt;");
  
              //Building the Header row.
              html.Append("&lt;tr&gt;");
              foreach (DataColumn column in dt.Columns)
              {
                  html.Append("&lt;th&gt;");
                  html.Append(column.ColumnName);
                  html.Append("&lt;/th&gt;");
              }
              html.Append("&lt;/tr&gt;");
  
              //Building the Data rows.
              foreach (DataRow row in dt.Rows)
              {
                  html.Append("&lt;tr&gt;");
                  foreach (DataColumn column in dt.Columns)
                  {
                      html.Append("&lt;td&gt;");
                      html.Append(row[column.ColumnName]);
                      html.Append("&lt;/td&gt;");
                  }
                  html.Append("&lt;/tr&gt;");
              }
  
              //Table end.
              html.Append("&lt;/table&gt;");
              string strText = html.ToString();
  
              ////Append the HTML string to Placeholder.
              placeholder.Controls.Add(new Literal { Text = html.ToString() });
  
          }
      }
  
      private DataTable GetData()
      {
          string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
          using (SqlConnection con = new SqlConnection(constr))
          {
              using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 [a01],[a02],[a03] FROM [aaa].[dbo].[bbb]"))
              {
                  using (SqlDataAdapter sda = new SqlDataAdapter())
                  {
                      cmd.Connection = con;
                      sda.SelectCommand = cmd;
                      using (DataTable dt = new DataTable())
                      {
                          sda.Fill(dt);
                          return dt;
                      }
                  }
              }
          }
      }
  }
  </code></pre>