In this blog we will discuss about how to create a datatable at runtime in asp.net using C#.Net.
Also check my previous articles on Tutorials on WCF in Asp.Net, Implement SQL Server authentication in asp.net and Difference between Abstract Class and Interface in 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);
Also check my previous articles on Tutorials on WCF in Asp.Net, Implement SQL Server authentication in asp.net and Difference between Abstract Class and Interface in 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);