Code to bind gridview with data reader

This code shows how to bind gridview with data reader
Use this code on page load

void Page_Load(Object sender, EventArgs e)
String Q;
Sql command cmd;
Sql DataReader dr;

SqlConnection conn = new SqlConnection();

conn.ConnectionString = ConfigurationManager.ConnectionStrings ["cn"].ConnectionString.ToString();


Q = "select * from tablename";

cmd= New Sqlcommand(Q, conn);

dr = cmd.ExecuteReader();

Gridview1.DataSource = dr;



Author: rajesh kumar parbat16 Jun 2010 Member Level: Gold   Points : 0

Nice one

Author: krishnasamy17 Jun 2010 Member Level: Gold   Points : 0

Good code snippet.

Guest Author: Enio Donci08 Mar 2012

how to get the number of current record ?

Guest Author: Mohamed eltigani17 Nov 2012

I think you should close the datareader.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: