Resources » Code Snippets » ASP.NET GridView

Code to bind gridview with data reader


Posted Date: 30-Sep-2008  Last Updated:   Category: ASP.NET GridView    
Author: Member Level: Gold    Points: 10



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


Did you like this resource? Share it with your friends and show your love!

Responses to "Code to bind gridview with data reader"
Author: rajesh kumar parbat    16 Jun 2010Member Level: Gold   Points : 0

Nice one



Author: krishnasamy    17 Jun 2010Member Level: Gold   Points : 0
Hi,
Good code snippet.



Guest Author: Enio Donci     08 Mar 2012
how to get the number of current record ?


Guest Author: Mohamed eltigani     17 Nov 2012
I think you should close the datareader.


Feedbacks      

Post Comment:




  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    sinha
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India