How is it then that I can bind a dataReader to a DataGrid and display multiple rows? I just don't get it. I currently have this code:
DbDataReader ddr = GetMetaClasses();
GridView1.DataSource = ddr;
GridView1.DataBind();
GetMetaClasses executes this stored procedure:
SELECT * FROM MetaClasses;
This is what my page displays:
| ID | Name | FriendlyName | MetaClassType |
|---|---|---|---|
| 1 | NewsItems | News | Article |
| 2 | ClientPage | Client Page | Page |
I don't understand how binding to the dataReader would work successfully and show all the results from the query if a dataReader can only hold one record in memory. Can someone please explain?? Thanks in advance!
Niradhip ChakrabortyPosted Aug 4, 2009, 6:16 AM
be bindable
2) DataReader is for reading data.So if some how u r able to bind
that,it won't allow u to edit that.So it's of no use.
3) DataReader is forward only,that means u can get a record at a time
but can't traverse back to the prev record.
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();
}
Kirtan PatelPosted Aug 4, 2009, 1:05 AM
it iterates through records one by one and All records Rendered One bye one in HTML Table row
so as a result you see data in Grid View .