2 dropdownlists using same datareader

T

tshad

Is there a way to databind a datareader to 2 different dropdownlists?
Something like:

StoredSearches.DataSource=objCmd.ExecuteReader
StoredSearches.DataValueField="SearchID"
StoredSearches.DataTextField= "SearchName"
StoredSearches.databind()
SearchList.DataSource=objCmd.ExecuteReader
SearchList.DataValueField="SearchID"
SearchList.DataTextField= "SearchName"
SearchList.databind()


I can't do the above as the second Execute reader gives me an error saying I
have to close the reader first - which would mean a re-read.

I want to do this as I only want to read the database once but show 2
different dropdownlist (in different ways depending on how the user selects
the page).

If I can't do this, can I just do:

Searchlist = StoredSearches

Thanks,

Tom
 
R

Richard

The behavior of ExecuteReader() is to execute the command and return an
IDataReader. An IDataReader can only be read through once.

Yes, you can bind SearchList to StoredSearches like so:

SearchList.DataSource = StoredSearch.Items
SearchList.DataTextField = "Text";
SearchList.DataValueField= "Value";
SearchList.DataBind();
 
S

Steve C. Orr [MVP, MCSD]

In addition to Richard's fine suggestion, you might want to return the
results in a DataTable (or DataSet) instead of a DataReader for improved
reusability. If the DataTable is used often you might store it in Session
or Application state to reduce round trips to the database and it will
always be within easy reach of your code.
 
T

tshad

Steve C. Orr said:
In addition to Richard's fine suggestion, you might want to return the
results in a DataTable (or DataSet) instead of a DataReader for improved
reusability. If the DataTable is used often you might store it in Session
or Application state to reduce round trips to the database and it will
always be within easy reach of your code.

Both suggestions are what I was looking for.

I assume I could also do a dual select in my stored procedure to return 2
result sets and bind to the first dropdown list, do a nextresult(), and then
bind to the 2nd dropdown.

Thanks,

Tom
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top