Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Show(Display) DataTable in HTML Table in Asp.Net from Database using C#, VB.NET

Apr 28, 2015
Introduction

Here I will explain how to show or display data from datatable in html table in
asp.net from database in c#, vb.net or display data from database in html table in asp.net using c#, vb.net with example or get data from datatable and show it in html table in asp.net using c#, vb.net.


Before we implement this example first design one table EmployeeInfo in your database as shown below


Now create one new web application and open your aspx page and write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Display Datatable in HTML Table in asp.net from database using c#, vb.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblResult" runat="server"/>
</div>
</form>
</body>
</html>

Now open aspx page codebehind behind file and add following namespaces

C# Code


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

After completion of adding namespaces you need to write the code like as shown below


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindHtmlTable();
}
}
// Bind HTML Table Data
private void BindHtmlTable()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB"))
{
using (SqlCommand cmd = new SqlCommand("select * from Employee_Details", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
}
StringBuilder htmlTable = new StringBuilder();
htmlTable.Append("<table border='1' cellpadding=4 cellspacing=0>");
htmlTable.Append("<tr>");
for (int i = 0; i < dt.Columns.Count; i++)
{
htmlTable.Append("<th>" + dt.Columns[i].ColumnName + "</th>");
}
htmlTable.Append("</tr>");
for (int j = 0; j < dt.Rows.Count; j++)
{
htmlTable.Append("<tr>");
htmlTable.Append("<td>" + dt.Rows[j]["userid"] + "</td>");
htmlTable.Append("<td>" + dt.Rows[j]["username"] + "</td>");
htmlTable.Append("<td>" + dt.Rows[j]["firstname"] + "</td>");
htmlTable.Append("<td>" + dt.Rows[j]["lastname"] + "</td>");
htmlTable.Append("<td>" + dt.Rows[j]["city"] + "</td>");
htmlTable.Append("<td>" + dt.Rows[j]["designation"] + "</td>");
htmlTable.Append("</tr>");
}
htmlTable.Append("</table>");
lblResult.Text = htmlTable.ToString();
}

VB.NET Code


Imports System.Data.SqlClient
Imports System.Data
Imports System.Text
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindHtmlTable()
End If
End Sub
' Bind HTML Table Data
Private Sub BindHtmlTable()
Dim dt As New DataTable()
Using con As New SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB")
Using cmd As New SqlCommand("select * from Employee_Details", con)
con.Open()
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
con.Close()
End Using
End Using
Dim htmlTable As New StringBuilder()
htmlTable.Append("<table border='1' cellpadding=4 cellspacing=0>")
htmlTable.Append("<tr>")
For i As Integer = 0 To dt.Columns.Count - 1
htmlTable.Append("<th>" & dt.Columns(i).ColumnName & "</th>")
Next
htmlTable.Append("</tr>")
For j As Integer = 0 To dt.Rows.Count - 1
htmlTable.Append("<tr>")
htmlTable.Append("<td>" & dt.Rows(j)("userid") & "</td>")
htmlTable.Append("<td>" & dt.Rows(j)("username") & "</td>")
htmlTable.Append("<td>" & dt.Rows(j)("firstname") & "</td>")
htmlTable.Append("<td>" & dt.Rows(j)("lastname") & "</td>")
htmlTable.Append("<td>" & dt.Rows(j)("city") & "</td>")
htmlTable.Append("<td>" & dt.Rows(j)("designation") & "</td>")
htmlTable.Append("</tr>")
Next
htmlTable.Append("</table>")
lblResult.Text = htmlTable.ToString()
End Sub
End Class

Demo

Display DataTable in HTML Table in ASP.Net using C#, VB.NET

Download Sample Code Attached

 

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Unknown said...

Hi this is superb code....I want to perform update and delete operation in above methods. I only have a Placeholder on my aspx design page and remaining code is on aspx.cs. i am using bootstrap. table.Append(a class='btn btn-warning' href='EditTransportVehicle.aspx?ID=" + rdr[0] +

Anonymous said...

By clicking on first cell, How to display the record the in Pop Up, Please do reply

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.