473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

New to .Net sqldatareader question

Rod
Thanks in advance.

I have two listboxes on a single asp page. I am trying to
use sqldatareader to populate both using two seperate sql
stored procs. I can populate each box seperately using
two different methods but when I try to populate them both
no the same page I get "InvalidOperationException: Invalid
attempt to FieldCount when reader is closed"

WHY???

Here is the code on the VB code behind page:

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
'pop1()
'pop2()

End Sub
Private Sub pop1()
Dim cnstr As String = "serverinfo is here"
Dim cn As New SqlConnection()
Dim cmd As New SqlCommand()
cn.ConnectionString = cnstr
Try

'*******Populate the Info Packets
'Set up Stored Proc
cmd.CommandText = "LookupFind"
cmd.CommandType() = CommandType.StoredProcedure
cmd.Connection = cn

'Pass Parameters
cmd.Parameters.Add("@LookupType", "Info")

'Execute
cn.Open()

Dim dr As SqlDataReader = cmd.ExecuteReader()

chkInfo().DataSource = dr
chkInfo().DataTextField = "LookupDesc"
chkInfo().DataValueField = "pkLookupId"
DataBind()

'Cleanup
dr.Close()

Catch

'Throw the previous exception to the caller
Throw

Finally

'Dispose is used in place of close
'Close is called by dispose
cn.Dispose()
cn = Nothing

End Try

End Sub

Private Sub pop2()
Dim cnstr As String = "serverinfo is here"
Dim cn As New SqlConnection()
Dim cmd As New SqlCommand()

cn.ConnectionString = cnstr

Try

'*******Populate the Info Packets
'Set up Stored Proc
cmd.CommandText = "ResortFill"
cmd.CommandType() = CommandType.StoredProcedure
cmd.Connection = cn

'Pass Parameters
'cmd.Parameters.Clear()

'Execute
cn.Open()

Dim dr As SqlDataReader = cmd.ExecuteReader()

chkResort().DataSource = dr
chkResort().DataTextField = "ResortName"
chkResort().DataValueField = "pkResortId"
DataBind()

'Cleanup
dr.Close()

Catch

'Throw the previous exception to the caller
Throw

Finally

'Dispose is used in place of close
'Close is called by dispose
cn.Dispose()
cn = Nothing

End Try

End Sub
Jul 21 '05 #1
2 4709
You should not be binding to a datareader.. Use a dataset, dataview or
datatable...
an alternative if it's not appropriate to bind to a dataset might be
something like this..

(warning: this is pseudo code)
With dr
While .Read
Dm objListItem As New
ListItem(.GetValue(.GetOrdinal("LookupDesc")),.Get Value(.GetOrdinal("pkLooku
pId")),))
ListBox2.Items.Add(Item)
End While
End With

When your page is rendering the controls datasource is enumerated during the
binding process, this won't work because your datareader is closed..
"Rod" <pu****@rpsconsulting.ca> wrote in message
news:42****************************@phx.gbl...
Thanks in advance.

I have two listboxes on a single asp page. I am trying to
use sqldatareader to populate both using two seperate sql
stored procs. I can populate each box seperately using
two different methods but when I try to populate them both
no the same page I get "InvalidOperationException: Invalid
attempt to FieldCount when reader is closed"

WHY???

Here is the code on the VB code behind page:

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
'pop1()
'pop2()

End Sub
Private Sub pop1()
Dim cnstr As String = "serverinfo is here"
Dim cn As New SqlConnection()
Dim cmd As New SqlCommand()
cn.ConnectionString = cnstr
Try

'*******Populate the Info Packets
'Set up Stored Proc
cmd.CommandText = "LookupFind"
cmd.CommandType() = CommandType.StoredProcedure
cmd.Connection = cn

'Pass Parameters
cmd.Parameters.Add("@LookupType", "Info")

'Execute
cn.Open()

Dim dr As SqlDataReader = cmd.ExecuteReader()

chkInfo().DataSource = dr
chkInfo().DataTextField = "LookupDesc"
chkInfo().DataValueField = "pkLookupId"
DataBind()

'Cleanup
dr.Close()

Catch

'Throw the previous exception to the caller
Throw

Finally

'Dispose is used in place of close
'Close is called by dispose
cn.Dispose()
cn = Nothing

End Try

End Sub

Private Sub pop2()
Dim cnstr As String = "serverinfo is here"
Dim cn As New SqlConnection()
Dim cmd As New SqlCommand()

cn.ConnectionString = cnstr

Try

'*******Populate the Info Packets
'Set up Stored Proc
cmd.CommandText = "ResortFill"
cmd.CommandType() = CommandType.StoredProcedure
cmd.Connection = cn

'Pass Parameters
'cmd.Parameters.Clear()

'Execute
cn.Open()

Dim dr As SqlDataReader = cmd.ExecuteReader()

chkResort().DataSource = dr
chkResort().DataTextField = "ResortName"
chkResort().DataValueField = "pkResortId"
DataBind()

'Cleanup
dr.Close()

Catch

'Throw the previous exception to the caller
Throw

Finally

'Dispose is used in place of close
'Close is called by dispose
cn.Dispose()
cn = Nothing

End Try

End Sub

Jul 21 '05 #2
>I have two listboxes on a single asp page. I am trying to
use sqldatareader to populate both using two seperate sql
stored procs.


A DataReader by design is a fast, forward-only mechanism to read your
data one row at a time. It does *not* give you back a set of rows
which could be used as a DataSource.

If you want to use DataReader, you'll have to read the data yourself,
store it in e.g. an ArrayList, and then use that ArrayList as the
DataSource for your comboboxes.

Alternatively, you can fill a DataTable with the values from your
stored proc, and then use that as a DataSource for your combo boxes -
the DataReader won't work.

Marc
================================================== ==============
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Jul 21 '05 #3

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

Similar topics

3
by: Ricola ! | last post by:
Why do I say: SqlDataReader dr; instead of SqlDataReader dr = new SqlDataReader();
3
by: PeteZ | last post by:
I'm having a problem where I exec a stored procedure (which SELECTs all rows from a table, which has 100 rows) - each row has 8 columns. When I exec the code (see below) I get 8 in...
7
by: Franck Diastein | last post by:
Hi, when I call ExportData I have this error: Invalid attempt to Read when reader is closed. Telling me that there's a problem with this line: while(_dataR.Read()){ Code:...
3
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the...
1
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not...
4
by: mimi | last post by:
Hi Please help me out, I can't find a way to close a sqldatareader when error occur at statement cmd.ExecuteReader(). I can't close it in catch because it is local in try scope and I can't...
2
by: SQL | last post by:
I'm new at this, so please forgive me if my question has a simple answer to it. I'm trying to get a value out of a SqlDataReader. Here is my code to do so: Dim mySQLCommand As New...
2
by: Rod | last post by:
Thanks in advance. I have two listboxes on a single asp page. I am trying to use sqldatareader to populate both using two seperate sql stored procs. I can populate each box seperately using...
3
by: Frank Milverckowitz | last post by:
Hi, Newbie question about SqlDataReader column value access... In java jdbc code to get a value from a table column we can pass the column name (instead of an int index or offset): String...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.