Add each row dynamically to a datalist or gridview?

Discussion in 'ASP .Net' started by Guest, Apr 25, 2006.

  1. Guest

    Guest Guest


    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
    Guest, Apr 25, 2006
    1. Advertisements

  2. Guest

    Guest 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:
    Guest, Apr 25, 2006
    1. Advertisements

  3. Guest

    Guest Guest

    Thank you Phillip.
    Guest, Apr 25, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.