How to Asp.Net Dynamic Dataset

In ADO.NET, you have the flexibility to create DataTable objects dynamically and add them to an existing DataSet. A DataTable, defined in the System.Data namespace, represents a single table of memory-resident data. It provides a structure to hold and manipulate data in a tabular format.

Dim dt As New DataTable dataset.Tables.Add(dt)
DataTable dt = new DataTable(); dataset.Tables.Add(dt);

A DataTable class is defined in the System.Data envelope and it may represent information that is resident in memory of once single table. As constraints for DataTable use the ‘PrimaryKey’ and ‘Unique’ properties.

The following ASP.NET program create a DataTable dynamically and add it into a Dataset.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <br /> <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox> </div> </form> </body> </html>
Full Source | C#
using System; using System.Data ; using System.Data.SqlClient ; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataTable dt; DataRow dr; DataColumn idCoulumn; DataColumn nameCoulumn; int i = 0; dt = new DataTable(); idCoulumn = new DataColumn("ID", Type.GetType("System.Int32")); nameCoulumn = new DataColumn("Name", Type.GetType("System.String")); dt.Columns.Add(idCoulumn); dt.Columns.Add(nameCoulumn); dr = dt.NewRow(); dr["ID"] = 1; dr["Name"] = "Name1"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 2; dr["Name"] = "Name2"; dt.Rows.Add(dr); ds.Tables.Add(dt); for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { ListBox1.Items.Add (ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]); } } }
Full Source | VB.NET
Imports System.Data Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ds As New DataSet Dim dt As DataTable Dim dr As DataRow Dim idCoulumn As DataColumn Dim nameCoulumn As DataColumn Dim i As Integer dt = New DataTable() idCoulumn = New DataColumn("ID", Type.GetType("System.Int32")) nameCoulumn = New DataColumn("Name", Type.GetType("System.String")) dt.Columns.Add(idCoulumn) dt.Columns.Add(nameCoulumn) dr = dt.NewRow() dr("ID") = 1 dr("Name") = "Name1" dt.Rows.Add(dr) dr = dt.NewRow() dr("ID") = 2 dr("Name") = "Name2" dt.Rows.Add(dr) ds.Tables.Add(dt) For i = 0 To ds.Tables(0).Rows.Count - 1 ListBox1.Items.Add(ds.Tables(0).Rows(i).Item(0) & " -- " & ds.Tables(0).Rows(i).Item(1)) Next i End Sub End Class


ADO.NET allows you to create DataTable objects dynamically and add them to an existing DataSet. The DataTable represents a single table of data and can be customized by adding columns and setting constraints. This provides a powerful mechanism for managing and manipulating data in a tabular format within your application.