Add each row dynamically to a datalist or gridview?

G

Guest

Hi,

I use an SqlDataReader to retrieve data and loop through it for a seperate
operation. At the same time I would like to read two of the several fields
into a Datagrid or Datalist. Is this possible with an Add row - Column 1 type
of command?

Or is there a way to bind the SqlDataReader Column 0 and 1.

I've tried binding like this and it does not work

gridview1.datasource = SqlDataReader.getsqlstring(0)

Any help would be great. Thanks
 
G

Guest

You can have a sqlDataReader assigned as the DataSource for a datagrid or a
datalist but once issue a databind you have to close that sqlDataReader
because it is a forward-only read-only stream of data that you cannot loop
through it again unless you close it, reconnect to the database and re-query
its command sql again.

So the best approach in your scenario is to retrieve the data once into a
dataset using a dataadapter. Since the dataset is disconnected you can both
databind it to a datagrid and still use it to loop through the data without
re-connecting to the database.

You might find this article of further help in understanding this topic:

http://msdn.microsoft.com/msdnmag/issues/04/06/DataPoints/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top