Fill Datatable and Bind GridView Using Data Reader in ASP.Net C#.Net VB.Net

In this article i am going to explain about how to fill the datatable using datareader and bind that datatable to gridview.

Datareader is read only forward only way of reading data. Compare to dataset datareader is fast in terms of reading data. To fill the datatable using datareader DataTable.Load() methaod is used in that datareader object is passed as the argument.

Create a new application and add a grid view in to display the details from the students table. Below is the html code used.

Fill Datatable Using Data Reader

For explanation purpose i have created a table called student and inserted some sample records. Below is the script used to create table and insert record into table.

Before moving ahead include the connection string element in the web.config file.

And now include the below namespaces to access the sqlconnection, datatable and configuration objects



On pageload event i am fetching the data from the students table using datareader and filling it to the datatable using DataTable.Load() method and then binding the same datatable to gridview. Below is the code.



The output will look like

Download Source

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!