Aspdotnet-Suresh

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

Bind Asp.net Gridview with DataReader in C#,VB.NET

Oct 2, 2012
Introduction:
 
In this article I will explain how to bind gridview with datareader in asp.net using C#.net and VB.NET

Description:

In previous post I explained Convert DataReader to DataTable, Send Gridview as Email body, Check/uncheck checkboxes in gridview using jQuery and many articles relating to asp.net, jQuery, SQL etc. Now I will explain how to bind gridview with datareader in asp.net using C#.net and VB.NET

DataReader

DataReader is used to read the data from database and it is a read and forward only connection oriented architecture during fetch the data from database. DataReader will fetch the data very fast when compared with dataset. Generally we will use ExecuteReader object to bind data to datareader.

To bind datareader data to gridview we need to write the code like as shown below

C# Code


// 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"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select UserName,LastName,Location FROM UserInformation", con);
SqlDataReader dr = cmd.ExecuteReader();
gvUserInfo.DataSource = dr;
gvUserInfo.DataBind();
con.Close();
}
}
VB.NET Code


' 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")
con.Open()
Dim cmd As New SqlCommand("Select UserName,LastName,Location FROM UserInformation", con)
Dim dr As SqlDataReader = cmd.ExecuteReader()
gvUserInfo.DataSource = dr
gvUserInfo.DataBind()
con.Close()
End Using
End Sub
If you want to see complete example for that check below post


  

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

Get the latest Asp.net, C#.net, 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

7 comments :

Candy Fredric said...

your post is very easy and clear to understand

Abdul Bassith said...

Hi Suresh, Good Job!! keep it up. this blog is very useful for Developers.

Vinu Natesh said...

hi suresh... i am your big fan...pls post the concept of cookies

Vinu Natesh said...

hi suresh... i am your big fan...pls post the concept of cookies

Mayur Sawant said...

Dear Suresh, I have a student database. I want simple VB.net code where on page load event, some fields (like stuname, age std, etc) from my db should get bind in label /txtbox. I have googled lot on internet but not find satisfactory result.

Please help
MayurS - sawantmayur2005@gmail.com

radhika tatamuri said...

Hi Suresh,
your website is simply super..Lot of information for developers..Thanks for the posts...Keep on posting..

K Vijayakumar said...

Hi Suresh,
Your post are simply superb.keep updating....

Give your Valuable Comments

Other Related Posts

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