473,698 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2 dropdownlists using same datareader

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

StoredSearches. DataSource=objC md.ExecuteReade r
StoredSearches. DataValueField= "SearchID"
StoredSearches. DataTextField= "SearchName "
StoredSearches. databind()
SearchList.Data Source=objCmd.E xecuteReader
SearchList.Data ValueField="Sea rchID"
SearchList.Data TextField= "SearchName "
SearchList.data bind()
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
Nov 19 '05 #1
3 1898
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.Data Source = StoredSearch.It ems
SearchList.Data TextField = "Text";
SearchList.Data ValueField= "Value";
SearchList.Data Bind();

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

StoredSearches. DataSource=objC md.ExecuteReade r
StoredSearches. DataValueField= "SearchID"
StoredSearches. DataTextField= "SearchName "
StoredSearches. databind()
SearchList.Data Source=objCmd.E xecuteReader
SearchList.Data ValueField="Sea rchID"
SearchList.Data TextField= "SearchName "
SearchList.data bind()
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


Nov 19 '05 #2
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.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"tshad" <ts**********@f tsolutions.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Is there a way to databind a datareader to 2 different dropdownlists?
Something like:

StoredSearches. DataSource=objC md.ExecuteReade r
StoredSearches. DataValueField= "SearchID"
StoredSearches. DataTextField= "SearchName "
StoredSearches. databind()
SearchList.Data Source=objCmd.E xecuteReader
SearchList.Data ValueField="Sea rchID"
SearchList.Data TextField= "SearchName "
SearchList.data bind()
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

Nov 19 '05 #3
"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote in message
news:e8******** ******@TK2MSFTN GP15.phx.gbl...
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
--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"tshad" <ts**********@f tsolutions.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Is there a way to databind a datareader to 2 different dropdownlists?
Something like:

StoredSearches. DataSource=objC md.ExecuteReade r
StoredSearches. DataValueField= "SearchID"
StoredSearches. DataTextField= "SearchName "
StoredSearches. databind()
SearchList.Data Source=objCmd.E xecuteReader
SearchList.Data ValueField="Sea rchID"
SearchList.Data TextField= "SearchName "
SearchList.data bind()
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


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
6697
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft Visual Basic .NET version of this article, see 308049. For a Microsoft Visual C++ .NET version of this article, see 310071. For a Microsoft Visual J# .NET version of this article, see 320627. This article refers to the following Microsoft .NET...
0
2126
by: theo | last post by:
Hello..I have a similar issue as yesterday but the situation has slightly changed so perhaps someone can shed some more light on the issue... Program flow - load file,extract xml text tags from file,then depending on the number of Xml tags retrieved from the file determines the number of dropdownlist controls instanciated in the placeholder,the user selects the required tags from the dropdownlists (if 5 Xml tags,then 5 dropdownlists...
4
3621
by: Craig Buchanan | last post by:
I would like to reuse a SQLDataReader that is populated with information as the datasource for multiple dropdownlists. Unfortunately, the first DDL closes the SDR and my code fails on the second DDR. Is there a way to copy/clone the SDR? Is there another solution? Thanks, Craig Buchanan
0
1395
by: Steve Caliendo | last post by:
Hi, I'm having trouble with multiple dynamically generated dropdownlists. On my web page I have several text boxes / labels / checkboxes and dropdownlists I have no problem setting the other controls to display what I would like them to, but the dropdownlist boxes are simply not working. I can't set the selectedindex to be different for each of the controls. What I'm doing is this:
9
5334
by: Timm | last post by:
I have an ASP.NET 2.0 page with two DropDownLists. I am using declarative data binding wherever possible and trying to minimize the use of code. The list of values in DropDownList DDL2 should be (filtered) dependent upon the selection in DDL1. I think this inevitably needs some code, but I'd be happy to be told otherwise! I have some code to handle OnSelectedIndexChanged for DDL1 that sets the FilterExpression associated with the...
4
1418
by: et | last post by:
How can I add a blank item to a drop down list so that it is the first item in the list? My drop down list is bound to a datareader, and if I add a blank item first as follows -- ddl.Items.Add(New ListItem(" ", 0)) -- then binding erases the blank item. Or maybe there is another way to solve my problem. An item in the drop down list is automatically selected by FindByValue:
3
1647
by: RSH | last post by:
Hi, I am having this very bizarre occurance with 4 dropdown lists on my ASP .Net page. Each control has its own unique id. When the user selects a value from each of the dropdownlists it is written correctly to the database. HOWEVER...When setting the SelectedIndex property of each of the controls in the codebehind (.net 1.1) I get an exception "A dropdownlist can not have
2
1504
by: sacha4 | last post by:
I need to make an edit page using data reader. this is my code.. Sub Page_Load() If Not IsPostBack Then Dim conn As SqlConnection Dim cmdSQL As SqlCommand Dim dataReader As SqlDataReader Dim SQL As String
1
1306
by: klaul | last post by:
I've been trying to do this for a day and a half now, and am clearly getting nowhere! The database I'm using is used to keep a log of service calls to various companies. When the user inserts data, they choose the company from a static list, but the reference number is user-supplied. The data is brought back using a GridView. I am trying to create two linked dropdownlists so that I can search the gridview (so that the items on the second...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.