Programmingempire
Although, there are many ways to create a DataTable object, in this article I will explain Creating a DataTable from a DataReader object. To begin with, first I will explain the DataReader object in brief.
DataReader in ADO.NET
Basically, a DataReader object in ADO.NET represents the connected approach of data access. A DataReader object provides read-only and forward-only access to data. Hence, the data access using DataReader is sequential. In other words, it is not possible to access any record randomly with DataReader.
DataTable in ADO.NET
Another class available in the ADO.NET library is DataTable that represents an in-memory table of data. Since we have this object in the application’s memory, it is possible to retrieve records in any order. Hence, we can use DataTable to get non-sequential access to data. furthermore, it is possible to bind the DataTable object to a Data Bound control in, ASP.NET such as GridView.
Load() Method of DataTable
As can be seen in the following code, we use the Load() method to fill the data values retrieved from a data source using an implementation of the IDataReader interface. In fact, the SqlDataReader class inherits from the abstract class DbDataRedaer. Further, the DbDataReader class implements the IDataRedaer interface. Hence, we can pass the object of SqlDataReader class as a parameter to the Load() method.
Example of Creating a DataTable from a DataReader
As an illustration, the WebForm just contains a GridView control.
WebForm1.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DataTableExample.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
WebForm1.aspx.cs
The following code shows how to access data from a database table using a connected approach. As a result of executing the select command of SQL, a DataReader object is returned. Later, we use this object to fill the DataTable object using the Load() method. Finally, the data binding is performed that loads the DataTable in GridView control.
using System;
using System.Data;
using System.Data.SqlClient;
namespace DataTableExample
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection c1;
SqlCommand cmd;
SqlDataReader dr;
DataTable dt;
public void Connect()
{
c1 = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersKAVITADocumentsd1.mdf;Integrated Security=True;Connect Timeout=30");
c1.Open();
}
public void CreateTable()
{
Connect();
cmd = new SqlCommand("select * from Items", c1);
dr = cmd.ExecuteReader();
dt = new DataTable();
dt.Load(dr);
}
protected void Page_Load(object sender, EventArgs e)
{
CreateTable();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
Output
Further Reading
Parameter and ParameterCollection in ADO.NET
Database Manipulation Using DataGrid
Example of Button and Link Button Control in ASP.NET
Example of Chart Control in ASP.NET
Creating a DataTable from a DataReader in ASP.NET
Deleting a Record using DataGrid Control in ASP.NET
Edit a Record Using DataGrid Control in ASP.NET
Insert a Record Using ItemCommand Event in DataGrid
CRUD Operations with DataGrid in ASP.NET
Creating Columns in a DataGrid Control
XML Documents and DataSet in ASP.NET
ASP.NET Core Features and Advantages
Display Images Using DataList Control
Adding Images Using Image Control
Creating a Group of Radio Buttons Using RadioButtonList Control
Example of Button Control in ASP.NET
ItemDataBound Event in DataList
More Features of DataList in ASP.NET
A Simple Example of Using a DataList Control in ASP.NET
Properties and Methods of DataList Control in ASP.NET
Exploring DataList Control in ASP.NET
Custom Validator Control in ASP.NET
Validation Summary Control in ASP.NET
Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator
An Example of Data Binding with RadioButtonList Control
Binding Data to Web Control in ADO.NET
Examples of AdRotator Control in ASP.NET