aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

ExecuteReader Example in Asp.Net using C#,VB.NET

Sep 21, 2012
In this article I will explain ExecuteReader example in using and VB.NET


In previous post I explained ExecuteScalar Example, ExecuteNonQuery Example and differences between ExecuteNonQuery, ExecuteReader and ExecuteScalar in Now I will explain ExecuteReader concept with one example in using, VB.NET


Execute Reader will be used to return the set of rows, on execution of SQL Query or Stored procedure using command object. This one is forward only retrieval of records and it is used to read the table values from first to last.

Before implement this example first design one table UserInformation in your database as shown below

Column Name
Data Type
Allow Nulls
Once table designed in database enter some dummy data to test after that write the following code in your aspx page 

<html xmlns="">
<head id="Head1" runat="server">
<title>Example of ExecuteReader in</title>
<form id="form1" runat="server">
<b>Bind Gridview with ExecuteReader command object Data</b><br /><br />
<asp:GridView ID="gvUserInfo" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White"/>
Now add the following namespaces in code behind

C# Code

using System;
using System.Data.SqlClient;
using System.Data;
After add namespaces write the following code in code behind

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

// This method is used to bind gridview from database
protected void BindGridview()
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
SqlCommand cmd = new SqlCommand("Select UserName,LastName,Location FROM UserInformation", con);
SqlDataReader dr = cmd.ExecuteReader();
gvUserInfo.DataSource = dr;

Imports System.Data.SqlClient
Imports System.Data

Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
End If
End Sub

' This method is used to bind gridview from database
Protected Sub BindGridview()
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
Dim cmd As New SqlCommand("Select UserName,LastName,Location FROM UserInformation", con)
Dim dr As SqlDataReader = cmd.ExecuteReader()
gvUserInfo.DataSource = dr
End Using
End Sub
End Class

If you observe above code I written select query like getting all the data from UserInfromation table but whenever we click on button we are getting only one value (First Row First Column value) and ignoring other row and column values.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


praneeth sampelly said...

Hi suresh my name is praneeth. I learned various concepts from your blog regarding .net where the faculty of my institute(pvt) failed to explain each and every concept clearly. Thank you very much for sharing your knowledge with persons like me and it was helpful to me very much....!

Anonymous said...

HI Suresh, Your blogs are a really too good. They have proved very helpful to me. Whenever I am stuck somewhere, i refer to ur blogs.
Thank You very much...

Anonymous said...

It is really good..

Anonymous said...

it's really very very good suresh.............its superb.....

Anonymous said...

Really its very good for beginners...

barkha S said...

Hi Suresh,

This is regarding post of Execute Data Reader in I want to tell you something and also want to know if we use using (sqlconnection con = new sqlconn........)
then we have to open the connection but don't have to close it. Am I right or not ???
Please clearify with explanation.

Lava Kumar said...

Hi Suresh , its really good for beginners .............................................. and thanks very much --------------------------------------kumar

Anonymous said...

you are not even using 3 tier structure and didnt even use parameters... this is a poor example which will create lots of coupling..

Suresh Dasari said...

Here we are not explaining about 3 tier structure or sending parameter we are explaining about ExecuteReader concept with example to make people understand concept easily....

Give your Valuable Comments

Other Related Posts

© 2010-2012 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.