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();

conn.Open();

Q = "select * from tablename";

cmd= New Sqlcommand(Q, conn);

dr = cmd.ExecuteReader();

Gridview1.DataSource = dr;

Gridview1.DataBind();
}


Comments

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


Nice one

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

Hi,
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.