Print Page | Close Window

Binding two controls to a SqlDataReader

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=6089
Printed Date: 29 March 2026 at 2:55am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Binding two controls to a SqlDataReader
Posted By: jamesb
Subject: Binding two controls to a SqlDataReader
Date Posted: 01 October 2003 at 11:41am

hi, I have two controls on a asp.net page, both of which I want to bind to different columns returned from a stored procedure. I can easily get one of the two to do bind, but not both - the code is as follows:

dim IntId As String
dim sqldata As SqlDataReader
  
IntId="4388"
  
sqldata = SqlHelper.ExecuteReader(connString, "sp_GetWhereHearListFromIntroducerID", IntId)
  
drop_wherehear_list.DataSource = sqldata
drop_wherehear_list.DataValueField = "ad_code"
drop_wherehear_list.DataBind()

Which works fine, but when I add the following code for the second dropdown I get an "Invalid attempt to FieldCount when reader is closed" error:

refid_list.DataSource = sqldata
refid_list.DataValueField = "bs_name"
refid_list.DataBind

The stored procedure returns two columns of data - surely it must be possible to bind another dropdown list to the other column, without having to do another round trip to the server?

Thanks for any help :)




Replies:
Posted By: Bunce
Date Posted: 03 October 2003 at 3:22am

I might get my syntax a little mixed up here but here goes!

The Execute Reader method will close the connection it uses after being 'iterated' through the first time.

Therefore when you try to bing to your second list box, the reader has already done its thing and can't be accessed any more.

You'll need to either execute the reader again, or use a container which holds your data for longer (such as the dataset).

Cheers,
Andrew 



-------------
There have been many, many posts made throughout the world...
This was one of them.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net