In this article I will explain with an example, how to populate (fill) DataTable using DataReader in ASP.Net using C# and VB.Net.
The DataTable will be populated with records from the DataReader using Load method of the DataTable.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Populate (Fill) DataTable using DataReader in ASP.Net
 
I have already inserted few records in the table.
Populate (Fill) DataTable using DataReader in ASP.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
 
 

Converting DataReader to DataTable using C# and VB.Net

Inside the Page_Load event handler, the records are fetched from the Customers Table of Northwind database.
First, a SqlDataReader class object is created and ExecuteReader method is called.
Finally, using Load method the records are loaded into the DataTable class object.
C#
protected void Page_Load(object sender, EventArgs e)
{
    string sql = "SELECT CustomerId, Name, Country FROM Customers";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                using (DataTable dt = new DataTable())
                {
                    dt.Load(sdr);
                }
            }
            con.Close();
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(sql, con)
            con.Open()
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                Using dt As DataTable = New DataTable()
                    dt.Load(sdr)
                End Using
            End Using
            con.Close()
        End Using
    End Using
End Sub
 
 

Screenshot

Populate (Fill) DataTable using DataReader in ASP.Net
 
 

Downloads