Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday 4 May 2015

Introduction: 
In this article I will explain how to show data from datatable in database in HTML table in ASP.NET(in C# and VB.NET).

Implementation:  Let's create a demo website to demonstrate the concept.
Create a database “Test” and then create one table i.e. “Emp_Personal”


Column Name
Data Type
Allow Nulls
Empper_id
Int(IDENTITY=TRUE)
No
EmpName
varchar(50)
Yes
Age
int
Yes
Address
varchar(50)
yes

Design your webpage as given below:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Table.aspx.cs" Inherits="Table" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Show Data in HTML Table from database</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblResult" runat="server"/>
</div>
</form>
</body>
</html>

ASP.NET(using C#) Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Data.SqlClient;
using System.Data;
using System.Text;


public partial class Table : System.Web.UI.Page
{
    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=localHost;Integrated Security=true;Initial Catalog=test"))
        {
            using (SqlCommand cmd = new SqlCommand("select * from Emp_personal", 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]["EmpPer_id"] + "</td>");
            htmlTable.Append("<td>" + dt.Rows[j]["Empname"] + "</td>");
            htmlTable.Append("<td>" + dt.Rows[j]["Age"] + "</td>");
            htmlTable.Append("<td>" + dt.Rows[j]["Address"] + "</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=localHost;Integrated Security=true;Initial Catalog=test")
Using cmd As New SqlCommand("select * from Emp_personal", 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)("Empper_id") & "</td>")
htmlTable.Append("<td>" & dt.Rows(j)("EmpName") & "</td>")
htmlTable.Append("<td>" & dt.Rows(j)("Age") & "</td>")
htmlTable.Append("<td>" & dt.Rows(j)("Address") & "</td>")
htmlTable.Append("</tr>")
Next
htmlTable.Append("</table>")
lblResult.Text = htmlTable.ToString()
End Sub
End Class
Categories: , ,

0 comments:

Post a Comment