<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>Display Data in Dynamically Created Table</title>
</head>
<body>
<form id="form1" runat="server">
<table style="width: 50%; text-align: center; background-color: skyblue;">
<tr>
<td align="center">
<asp:PlaceHolder ID="DBDataPlaceHolder" runat="server"></asp:PlaceHolder>
</td>
</tr>
</table>
</form>
</body>
</html>
<!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>Display Data in Dynamically Created Table</title>
</head>
<body>
<form id="form1" runat="server">
<table style="width: 50%; text-align: center; background-color: skyblue;">
<tr>
<td align="center">
<asp:PlaceHolder ID="DBDataPlaceHolder" runat="server"></asp:PlaceHolder>
</td>
</tr>
</table>
</form>
</body>
</html>
..............................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
SqlDataAdapter da;
DataSet ds = new DataSet();
StringBuilder htmlTable = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindData();
}
private void BindData()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=COMTECH-PC\SQLEXPRESS;Integrated Security=true;Initial Catalog=kabir";
SqlCommand cmd = new SqlCommand("SELECT * FROM employee", con);
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
htmlTable.Append("<table border='1'>");
htmlTable.Append("<tr style='background-color:green; color: White;'><th>ID.</th><th>Name</th><th>Salary</th></tr>");
if (!object.Equals(ds.Tables[0], null))
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
htmlTable.Append("<tr style='color: White;'>");
htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["ID"] + "</td>");
htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Name"] + "</td>");
htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Salary"] + "</td>");
htmlTable.Append("</tr>");
}
htmlTable.Append("</table>");
DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });
}
else
{
htmlTable.Append("<tr>");
htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");
htmlTable.Append("</tr>");
}
}
}
}
No comments:
Post a Comment