Create Table Programmatically in ASP.NET

Introduction

In this article we will create a table in a web application programmatically. Instead of a database table we will create a table in a code behind file using the DataTable class. We can add rows in this table dynamically from front ends through user input as many as we want. Basically this technique is used for adding and showing temporary data at run time.

All the code related to this mini application is given below. So let's start to create this web application.

Step 1: Open Visual Studio 2010.

  • Go to Visual Studio 2010
  • New-> Select a website application
  • Click OK

img1.gif

Step 2: Now we will add a new aspx page to our project; do the following for that:

  • Go to the Solution Explorer
  • Right-click on the Project name
  • Select add new item
  • Add new web page and give it a name
  • Click OK

img2.gif

Step 3: This is the code of the .aspx file that determines how our page looks:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body style=" background-color:Yellow">  
  8.     <form id="form1" runat="server">  
  9.     <div >  
  10.    <asp:gridview runat="server" ID="Gv1" AutoGenerateColumns="true" HeaderStyle-BackColor="Red" BackColor="LightBlue"  
  11.     BorderWidth="5" BorderColor="Blue">  
  12.    </asp:gridview>  
  13.     </div>  
  14.     <div>  
  15.     <h1>Add New Row.....</h1>  
  16.         Product NO:       
  17.     <asp:TextBox ID="txtb1" runat="server"></asp:TextBox>  
  18.         <br />  
  19.         Product Name:   
  20.      <asp:TextBox ID="txtb2" runat="server"></asp:TextBox>  
  21.         <br />  
  22.         Order Date:        
  23.       <asp:TextBox ID="txtb3" runat="server"></asp:TextBox>  
  24.        <asp:Label runat="server" Text="dd/mm/yyyy"></asp:Label>  
  25.         <br />      Quantity:            
  26.        <asp:TextBox ID="txtb4" runat="server"></asp:TextBox>  
  27.         <br />  
  28.         <br />  
  29.                  
  30.               
  31.           
  32.        <asp:Button runat="server" OnClick="Addnewrow" Text="Add Row" />  
  33.     </div>  
  34.     </form>  
  35. </body>  
  36. </html> 

Step 4: This is the code behind file code. It contains the logic of application:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. public partial class Default2 : System.Web.UI.Page  
  9. {  
  10.     DataTable tb = new DataTable();  
  11.     DataRow dr;  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.         if(!IsPostBack)  
  15.         createtable();  
  16.     }  
  17.     public void createtable()  
  18.     {  
  19.         tb.Columns.Add("Prod_NO"typeof(string));  
  20.         tb.Columns.Add("Prod_Name"typeof(string));  
  21.         tb.Columns.Add("Order_Date"typeof(string));  
  22.         tb.Columns.Add("Quantity"typeof(string));  
  23.         dr = tb.NewRow();  
  24.         dr["Prod_NO"] = "101";  
  25.         dr["Prod_Name"] = "Product1";  
  26.         dr["Order_Date"] = "12/06/2012";  
  27.         dr["Quantity"] = "50";  
  28.         tb.Rows.Add(dr);  
  29.         dr = tb.NewRow();  
  30.         dr["Prod_NO"] = "102";  
  31.         dr["Prod_Name"] = "Product2";  
  32.         dr["Order_Date"] = "15/06/2012";  
  33.         dr["Quantity"] = "70";  
  34.         tb.Rows.Add(dr);  
  35.         Gv1.DataSource = tb;  
  36.         Gv1.DataBind();  
  37.         ViewState["table1"] = tb;  
  38.     }  
  39.     protected void Addnewrow(object sender, EventArgs e)  
  40.     {  
  41.         tb =(DataTable) ViewState["table1"];  
  42.         dr = tb.NewRow();  
  43.         dr["Prod_NO"] = txtb1.Text;  
  44.         dr["Prod_Name"] = txtb2.Text;  
  45.         dr["Order_Date"] = txtb3.Text;  
  46.         dr["Quantity"] = txtb4.Text;  
  47.         tb.Rows.Add(dr);  
  48.         Gv1.DataSource = tb;  
  49.         Gv1.DataBind();  
  50.         txtb1.Text = "";  
  51.         txtb2.Text = "";  
  52.         txtb3.Text = "";  
  53.         txtb4.Text = "";  
  54.     }  
  55. } 

Step 5: After running the application, it looks like this:

img3.gif

To add a new row you will fill in all the text boxes and click the Add Row button:

img4.gif

Now you can see one row added in the GridView:

img5.gif


Similar Articles