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. |
|||
|
|||
7 comments :
your post is very easy and clear to understand
Hi Suresh, Good Job!! keep it up. this blog is very useful for Developers.
hi suresh... i am your big fan...pls post the concept of cookies
hi suresh... i am your big fan...pls post the concept of cookies
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
Hi Suresh,
your website is simply super..Lot of information for developers..Thanks for the posts...Keep on posting..
Hi Suresh,
Your post are simply superb.keep updating....
Note: Only a member of this blog may post a comment.