In this article I will explain with an example, how to load / fill/ populate DataTable from Stored Procedure in ASP.Net using C# and VB.Net.
The records returned from the Stored Procedure will be loaded into the DataTable using ADO.Net SqlDataAdapter in C# and VB.Net
 
 
Database
I have made use of the following table Customers with the schema as follows.
Load / Fill / Populate DataTable from Stored Procedure in ASP.Net using C# and VB.Net
I have already inserted few records in the table.
Load / Fill / Populate DataTable from Stored Procedure in ASP.Net using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Stored Procedure
The following Stored Procedure fetches the records from Customers Table.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetCustomers]
AS
BEGIN
      SET NOCOUNT ON;
 
      SELECT CustomerId
            ,Name
            ,Country
      FROM Customers
END
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Bind / Fill / Populate DataTable from Database
Inside the Page Load event of the page, the DataTable is loaded with the records of the Customers Table returned from the Stored Procedure.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("GetCustomers"))
            {
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("GetCustomers")
                cmd.Connection = con
                cmd.CommandType = CommandType.StoredProcedure
                Using sda As New SqlDataAdapter(cmd)
                    Dim dt As New DataTable()
                    sda.Fill(dt)
                End Using
            End Using
        End Using
    End If
End Sub
 
 
Screenshot
The following screenshot displays the DataTable loaded with records returned from Stored Procedure.
Load / Fill / Populate DataTable from Stored Procedure in ASP.Net using C# and VB.Net
 
 
Downloads