Create a data table at runtime in Asp.Net

According to Microsoft A DataSet is made up of a collection of tables, relationships, and constraints. In ADO.NET, DataTable objects are used to represent the tables in a DataSet. A DataTable represents one table of in-memory relational data; the data is local to the .NET-based application in which it resides but can be populated from a data source such as Microsoft SQL Server using a DataAdapter.

We will see here, how to create a datatable at runtime in asp.net using C#.Net.

Create a datatable at runtime in asp.net using C#.Net

Now we will see how to create a datatable at runtime in asp.net using c#.net.

Below is the namespace needed.

using System.Data;

// Create a DataTable instance
DataTable dataTable = new DataTable("DataTableName");
// Create a DataColumn instances
DataColumn dtCol1 = new DataColumn();
DataColumn dtCol2 = new DataColumn();
dtCol1.ColumnName = "Id";
dtCol1.DataType = Type.GetType("System.Int32");

dtCol2.ColumnName = "Name";
dtCol2.DataType = Type.GetType("System.String");

// Add these DataColumns into the DataTable
dataTable.Columns.Add(dtCol1);
dataTable.Columns.Add(dtCol2);

// Create a DataRow Instance from the table we create above, with NewRow();
DataRow row = dataTable.NewRow();

row["Id"] = 1;
row["Name"] = "AspDotNetHelp";

// Add the row into the table
dataTable.Rows.Add(row);

You may like following asp.net tutorials:

Hope this asp.net tutorial explains how to create data table at runtime in Asp.Net using c#.net.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>