Display Data Using Table Control

Satyapriyanayak
Posted by Satyapriyanayak under ASP.NET category on | Points: 40 | Views : 802
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Display_Data_Using_Table_Control._Default" %>

<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server"
Text="Display Records Through table control" onclick="Button1_Click" />
<asp:Table ID="Table1" runat="server" BackColor="#FFFF80" BorderColor="#FF8080"
BorderStyle="Solid" BorderWidth="5px" ForeColor="Red" GridLines="Both">
</asp:Table><br />
</div>
</form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Display_Data_Using_Table_Control
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
DataTable dt;
int i;
int j;

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
try
{
con.Open();
str = "select * from student ";
com = new SqlCommand(str, con);
ds = new DataSet();
sqlda = new SqlDataAdapter(com);
sqlda.Fill(ds, "student");
dt = ds.Tables["student"];

for (i = 0; i <= dt.Rows.Count - 1; i++)
{
TableRow tr = new TableRow();

for (j = 0; j <= dt.Columns.Count - 1; j++)
{
TableCell tc = new TableCell();
tc.Text = dt.Rows[i].ItemArray[j].ToString();
tr.Cells.Add(tc);
Table1.Rows.Add(tr);
}
}
con.Close();

}
catch (Exception ex)
{
Response.Write(ex.Message);
}

}
}
}

Comments or Responses

Login to post response