top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to display data from database in HTML table in ASP.Net

+1 vote
2,256 views
How to display data from database in HTML table in ASP.Net
posted Dec 28, 2015 by Jayshree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

HTML Markup
The HTML Markup consists of a PlaceHolder within which the HTML Table will be displayed.

<asp:PlaceHolder ID = "PlaceHolder1" runat="server" />

Namespaces
You will need to import the following namespaces.
C#

using System.Data;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Data
Imports System.Text
Imports System.Configuration
Imports System.Data.SqlClient

Displaying data from database in HTML Table using C# and VB.Net
Inside the Page Load event of the ASP.Net Page, first the DataTable is populated with the records from the Customers table.
Then using the StringBuilder class, a string consisting of an HTML Table is generated by iterating through the columns and the rows of the DataTable.
Finally the HTML string is displayed on the page by adding the Literal control to the PlaceHolder.
C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
       //Populating a DataTable from database.
        DataTable dt = this.GetData();

        //Building an HTML string.
        StringBuilder html = new StringBuilder();

        //Table start.
        html.Append("<table border = '1'>");

        //Building the Header row.
        html.Append("<tr>");
        foreach (DataColumn column in dt.Columns)
        {
            html.Append("<th>");
            html.Append(column.ColumnName);
            html.Append("</th>");
        }
        html.Append("</tr>");

        //Building the Data rows.
        foreach (DataRow row in dt.Rows)
        {
            html.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                html.Append("<td>");
               html.Append(row[column.ColumnName]);
                html.Append("</td>");
            }
            html.Append("</tr>");
        }

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

        //Append the HTML string to Placeholder.
        PlaceHolder1.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 CustomerId, Name, Country FROM Customers"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        'Populating a DataTable from database.
        Dim dt As DataTable = Me.GetData()

        'Building an HTML string.
        Dim html As New StringBuilder()

        'Table start.
        html.Append("<table border = '1'>")

        'Building the Header row.
        html.Append("<tr>")
        For Each column As DataColumn In dt.Columns
            html.Append("<th>")
            html.Append(column.ColumnName)
            html.Append("</th>")
        Next
        html.Append("</tr>")

        'Building the Data rows.
        For Each row As DataRow In dt.Rows
            html.Append("<tr>")
            For Each column As DataColumn In dt.Columns
                html.Append("<td>")
                html.Append(row(column.ColumnName))
                html.Append("</td>")
            Next
            html.Append("</tr>")
        Next

        'Table end.
        html.Append("</table>")

        'Append the HTML string to Placeholder.
       PlaceHolder1.Controls.Add(New Literal() With { _
          .Text = html.ToString() _
        })
    End If
End Sub

Private Function GetData() As DataTable
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers")
            Using sda As New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dt As New DataTable()
                    sda.Fill(dt)
                    Return dt
                End Using
            End Using
        End Using
    End Using
End Function
answer Dec 28, 2015 by Shivaranjini
Similar Questions
+3 votes

I want to save specific html portion into pdf using asp.net into MS SQL SERVER 2008. I know it will done will itextsharp.dll library file from .NET conversion but tell me through example.

...