How to create html table from code behind?

In this article I will explain how to create the html table from c# code behind and how to show database record in it.I am using .net framework 4 and sqlserver 2008 r2. you can work with any version you I am creating a simple html table which will show the record.You can set the style of the table so that it looks well.

First of all thanks for taking your interest in my article.Here I am describing how to create html table from code behind and store data in it.
first of all take the empty web application from project template and give it any name.

Now add a new webform to the website by right clicking the project name>add new item>choose webform>
give the name to the webform as Default.aspx
and click add.

as your .net set up is complete now open sqlserver management studio.
Create a database having name html and add a table having name thtml to it.
thtml contains three columns id,name,rollno. fill some data in the table.

for creating database open new query window and paste the below query:
create database html
select this query and execute this will creat the database having name html.

Now we have to add the table to right click your database name select new query and create the table with below query:

create table thtml(id int,name varchar(50),rollno varchar(50))
select the full query and click execute.this will create the table having name thtml of three columns id,name,rollno.We will show the record of this table into html table.

now fill some data in it with

insert into thtml(id,name,rollno) values(1,'firstname','123')
insert into thtml(id,name,rollno) values(2,'hello','786')
insert into thtml(id,name,rollno) values(3,'ram','89')
insert into thtml(id,name,rollno) values(4,'seema','97')
insert into thtml(id,name,rollno) values(5,'kabir','234')
Now your database work is complete

AS our database work in complete,back to .net open default.aspx.cs.

here we will write our code.
First of all add two name spaces at the top of page:

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

now we are creating a function which will create your html table like below.I hope you are able to write your connection string.If not don't worry.Go to view select server explorer.right click dataconnection and click Add Connection and then add the connection give the name of your server in server name(as it shows while you open sqlserver management studio) and select the authentication mode which you selected while creating the database in sqlserver then choose your database name and click ok.
now right click this connection then go to properies and copy the full connection string form there.

public void createtable()
SqlConnection con = new SqlConnection(@"Paste Your ConnectionString here");
SqlCommand cmd = new SqlCommand("select name,rollno from thtml", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();

//now we filled our data table
//below is the code to create html table
string t = string.Empty;
t = "";
t += "";
for (int i = 0; i <= dt.Rows.Count - 1; i++)
t += "
t += "
" + dt.Rows[i]["name"].ToString() + "" + dt.Rows[i]["rollno"].ToString() + "

now we call this function on the page load event like below:

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)


Now your work is complete just run the will se the html table on the web browser which shows your record.I am attaching the sample with it.

Hope you like this article.

Thanks and Regards
Shalini Rathore


  • How to create html table from code behind (45624-31414-CreateHtmlTable.rar)
  • Comments

    No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: