How to create a table in Asp.net using C#

        How to create a table in Asp.net using C#


create table in asp net







The datatable class represents the tables and provides a collection of columns and rows to store data in database or gridview. In this tutorial how to create a datatable and run in c#. And learn how to create a columns and rows using DataTable and bind a table to view in Gridview control.

DataTable Class:-

  • Columns - Represents all table columns 
  • DataSet - Returns the dataset for the table
  • Rows - All rows of the data table
  • Primary key - gets or sets an array of columns as the primary key for the table
  • constraints - Returns the constraints collection.
  • childRelations - Returns the collection of child relationship
  • parentRelations - Returns of the ParentsRelation collections.
  • alot of things we do on DataTable But we see some examples in this tutorial.

Take the following steps :−

-> Start Visual Studio.
-> On the menu bar, choose File -> New -> Project.
-> Choose  ASP .NET web Form
-> Right sidebar solution Explorer -> Right click -> choose Add->WebForm
-> Specify a name for your project and click OK button. 

create table in asp net





Program :-

Default.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">

        </asp:GridView>
    
    </div>
    </form>
</body>
</html>



Default.aspx.cs

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = CreateDataSet();
            GridView1.DataSource = ds.Tables["cars"];
            GridView1.DataBind();
        }

    }
    private DataSet CreateDataSet()
    { 
         //creating a Dataset objects for tables
        
        DataSet dataset = new DataSet();
        
        //creating a cars table

        DataTable cars = CreateCarsTable();
        dataset.Tables.Add(cars);
        return dataset;
    }
    private DataTable CreateCarsTable() 
    {
        DataTable cars = new DataTable("cars");

        //adding columns

        AddNewColumn(cars, "System.Int32", "CarsID");
        AddNewColumn(cars, "System.String", "CarsName");
        AddNewColumn(cars, "System.String", "Price");

        //adding rows

        AddNewRow(cars, 1, "Renault Duster","8L");
        AddNewRow(cars, 2, "Renault Xuv", "5L"); 
        AddNewRow(cars, 3, "Renault Desire", "12L");
        AddNewRow(cars, 4, "Renault Triber", "8L");
        AddNewRow(cars, 5, "Renault Xuv300", "10L");
        AddNewRow(cars, 6, "Renault Desire z", "5.25L");

        return cars;


    }
    private void AddNewColumn(DataTable table, string columnType, string columnName)
    {
        DataColumn column = table.Columns.Add(columnName, Type.GetType(columnType));
    }
    //adding data into table

    private void AddNewRow(DataTable table, int id, string name, string price)

    {
        DataRow newrow = table.NewRow();
        newrow["CarsID"]=id;
        newrow["CarsName"]=name;
        newrow["Price"]=price;
        table.Rows.Add(newrow);
    }

    }
        
output:-

create table in asp net









Previous Post Next Post