Home Asp.net Ado.net – SqlCommand ExecuteReader Example in Asp.net C# Vb.net

Ado.net – SqlCommand ExecuteReader Example in Asp.net C# Vb.net


In my previous tutorials, I’d explained about ExecuteScalar, ExecuteNonQuery and Difference between ExecuteReader ExecuteNonQuery and ExecuteScalar and other more cracking tutorials on Asp.net here.

Now here in this tutorial, I’ll explain Ado.net ExecuteReader function in detail with example code.

The SqlCommand ExecuteReader in Ado.net is used to get and read the set of rows fetched using sql queries or stored procedures from the sql database table.

ExecuteReader object is a forward-only and supports read-only access of queried results that fetched from the database tables cannot be modified as it always opens in read-only mode.

To explain further about execute reader example, we need to create database table to read data and bind retrieved result to asp.net gridview, so simply execute following script to sql query editor to create database table and then add few records manually or download complete example code with script at the end of the page.

Here is the script to create “SubjectDetails” table:

CREATE TABLE [dbo].[SubjectDetails]
    [SubjectId] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [SubjectName] [nvarchar](100) NULL

Ado.net SqlCommand ExecuteReader Example in .NET – [.aspx]

Following is the complete HTML Markup for your .aspx page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Ado.net - ExecuteReader Example in C# .NET</title>
    <form id="form1" runat="server">
            <td colspan="3">
                <h4>SqlCommand ExecuteReader in ado.net example</h4>
            <td colspan="3">
                <asp:GridView ID="gvSubjectDetails" runat="server" AutoGenerateColumns="false"
                    <HeaderStyle Font-Bold="true" BackColor="#ff6600" BorderColor="#f5f5f5"
                        ForeColor="White" Height="30" />
                        <asp:BoundField DataField="SubjectID" HeaderText="Subject Id"
                            ItemStyle-Width="150px" />
                        <asp:BoundField DataField="SubjectName" HeaderText="Subject Name"
                            ItemStyle-Width="200px" />
Note: Required namespace for ExecuteReader() is System.Data.SqlClient.

Now check the below sample example code snippet to bind the asp.net grid view with executereader.

Ado.net SqlCommand ExecuteReader Example – [C#]

//specify your connection string here..
string strConn = "Data Source=datasource;Integrated Security=true;Initial Catalog=yourDB";

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {

//bind subject details to gridview
private void BindGridviewFileData() {
    try {
        using (SqlConnection sqlConn = new SqlConnection(strConn)) {
            using (SqlCommand sqlCmd = new SqlCommand()) {
                sqlCmd.CommandText = "SELECT * FROM SubjectDetails";
                sqlCmd.Connection = sqlConn;
                SqlDataReader objDataReader = sqlCmd.ExecuteReader();
                gvSubjectDetails.DataSource = objDataReader;
    } catch { }
Check this also: How to bind data to asp.net gridview using ExecuteReader? and get more cracking tutorials on Gridivew here.

Ado.net SqlCommand ExecuteReader Example – [Vb.net]

'specify your connection string here..'
Dim strConn As String = "Data Source=datasource;Integrated Security=true;Initial Catalog=yourDB"

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
    End If
End Sub

'bind subject details to gridview'
Private Sub BindGridviewFileData()
        Using sqlConn As New SqlConnection(strConn)
            Using sqlCmd As New SqlCommand()
                sqlCmd.CommandText = "SELECT * FROM SubjectDetails"
                sqlCmd.Connection = sqlConn
                Dim objDataReader As SqlDataReader = sqlCmd.ExecuteReader()
                gvSubjectDetails.DataSource = objDataReader
            End Using
        End Using
    End Try
End Sub

Example Result

Ado.net - SqlCommand ExecuteReader Example in Asp.net C# Vb.net
Ado.net – SqlCommand ExecuteReader Example in Asp.net C# Vb.net

Download Example

Git Repo

That’s it, this is one of the simplest way to understand ado.net sqlcommand executereader in .net using c# or vb.net.

Let me know if you’ve any questions or doubts about this tutorial by writing down your queries in comment below. I would be happy to provide you my feedback as soon as possible.

Happy Coding!

Previous articleAdo.net – SqlCommand ExecuteNonQuery Example in Asp.net C# Vb.net
Next articleMain Difference Between ExecuteReader, ExecuteNonQuery, ExecuteScalar
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.


Please enter your comment!
Please enter your name here

15 − 6 =