473,394 Members | 1,739 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,394 software developers and data experts.

Two DataReaders, one Connection

Good day for everybody!

I new .NET developer... How I can to do this:

Dim cmdCommand1 As SqlCommand
Dim cmdCommand2 As SqlCommand

Dim dataReader1 As SqlDataReader
Dim dataReader2 As SqlDataReader

cmdCommand1 = New SqlCommand("SELECT * FROM Table1", cnnSPE)

cnn.Open()

dataReader1 = cmdCommand1.ExecuteReader
Do While dataReader1.Read

cmdCommand2 = New SqlCommand("Select * from Table2 Where Field1=" &
dataReader1("Field1"), cnn)
dataReader2 = cmdCommand2.ExecuteReader

While drCandidatos.Read

End While
dataReader1.Close()
Loop
cnn.Close()

When i execute the program show this error:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.data.dll
Additional information: There is already an open DataReader associated with
this Connection which must be closed first.

Are there other solution for that?

Thanks for your help.

Dec 15 '05 #1
4 6324
Hello,

You can only have one DataReader open per connection. In your case,would
it not be easier to write your query as a join over both "Table1" and
"Table2"? This way, you only need to open one reader, and overall
performance would be a lot better

HTH,

Bennie Haelen

WilliamR wrote:
Good day for everybody!

I new .NET developer... How I can to do this:

Dim cmdCommand1 As SqlCommand
Dim cmdCommand2 As SqlCommand

Dim dataReader1 As SqlDataReader
Dim dataReader2 As SqlDataReader

cmdCommand1 = New SqlCommand("SELECT * FROM Table1", cnnSPE)

cnn.Open()

dataReader1 = cmdCommand1.ExecuteReader
Do While dataReader1.Read

cmdCommand2 = New SqlCommand("Select * from Table2 Where Field1=" &
dataReader1("Field1"), cnn)
dataReader2 = cmdCommand2.ExecuteReader

While drCandidatos.Read

End While
dataReader1.Close()
Loop
cnn.Close()

When i execute the program show this error:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.data.dll
Additional information: There is already an open DataReader associated with
this Connection which must be closed first.

Are there other solution for that?

Thanks for your help.

Dec 15 '05 #2
Thank you Benny... In my case not is easy:
In the first Table i need to read all records one to one (each record have a
column with a value); for each record to seek in the second Table; in this, i
need to take the amount of columns = to the value that read in the first
Table.

I think to read the firts table and storage the fields that i need in one
array; and then read the array, so i can to open a Datareader.

excuse me for my english.

"Bennie Haelen" wrote:
Hello,

You can only have one DataReader open per connection. In your case,would
it not be easier to write your query as a join over both "Table1" and
"Table2"? This way, you only need to open one reader, and overall
performance would be a lot better

HTH,

Bennie Haelen

WilliamR wrote:
Good day for everybody!

I new .NET developer... How I can to do this:

Dim cmdCommand1 As SqlCommand
Dim cmdCommand2 As SqlCommand

Dim dataReader1 As SqlDataReader
Dim dataReader2 As SqlDataReader

cmdCommand1 = New SqlCommand("SELECT * FROM Table1", cnnSPE)

cnn.Open()

dataReader1 = cmdCommand1.ExecuteReader
Do While dataReader1.Read

cmdCommand2 = New SqlCommand("Select * from Table2 Where Field1=" &
dataReader1("Field1"), cnn)
dataReader2 = cmdCommand2.ExecuteReader

While drCandidatos.Read

End While
dataReader1.Close()
Loop
cnn.Close()

When i execute the program show this error:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.data.dll
Additional information: There is already an open DataReader associated with
this Connection which must be closed first.

Are there other solution for that?

Thanks for your help.

Dec 15 '05 #3
Bottom line:

Why not just open two (2) SqlConnection's ?
WilliamR wrote:
Thank you Benny... In my case not is easy:
In the first Table i need to read all records one to one (each record have a
column with a value); for each record to seek in the second Table; in this, i
need to take the amount of columns = to the value that read in the first
Table.

I think to read the firts table and storage the fields that i need in one
array; and then read the array, so i can to open a Datareader.

excuse me for my english.

"Bennie Haelen" wrote:

Hello,

You can only have one DataReader open per connection. In your case,would
it not be easier to write your query as a join over both "Table1" and
"Table2"? This way, you only need to open one reader, and overall
performance would be a lot better

HTH,

Bennie Haelen

WilliamR wrote:
Good day for everybody!

I new .NET developer... How I can to do this:

Dim cmdCommand1 As SqlCommand
Dim cmdCommand2 As SqlCommand

Dim dataReader1 As SqlDataReader
Dim dataReader2 As SqlDataReader

cmdCommand1 = New SqlCommand("SELECT * FROM Table1", cnnSPE)

cnn.Open()

dataReader1 = cmdCommand1.ExecuteReader
Do While dataReader1.Read

cmdCommand2 = New SqlCommand("Select * from Table2 Where Field1=" &
dataReader1("Field1"), cnn)
dataReader2 = cmdCommand2.ExecuteReader

While drCandidatos.Read

End While
dataReader1.Close()
Loop
cnn.Close()

When i execute the program show this error:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.data.dll
Additional information: There is already an open DataReader associated with
this Connection which must be closed first.

Are there other solution for that?

Thanks for your help.

Dec 15 '05 #4
I use two connect to solve this problem.

^_^
Tony
"WilliamR" <Wi******@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
Thank you Benny... In my case not is easy:
In the first Table i need to read all records one to one (each record have
a
column with a value); for each record to seek in the second Table; in
this, i
need to take the amount of columns = to the value that read in the first
Table.

I think to read the firts table and storage the fields that i need in one
array; and then read the array, so i can to open a Datareader.

excuse me for my english.

"Bennie Haelen" wrote:
Hello,

You can only have one DataReader open per connection. In your case,would
it not be easier to write your query as a join over both "Table1" and
"Table2"? This way, you only need to open one reader, and overall
performance would be a lot better

HTH,

Bennie Haelen

WilliamR wrote:
> Good day for everybody!
>
> I new .NET developer... How I can to do this:
>
> Dim cmdCommand1 As SqlCommand
> Dim cmdCommand2 As SqlCommand
>
> Dim dataReader1 As SqlDataReader
> Dim dataReader2 As SqlDataReader
>
> cmdCommand1 = New SqlCommand("SELECT * FROM Table1", cnnSPE)
>
> cnn.Open()
>
> dataReader1 = cmdCommand1.ExecuteReader
> Do While dataReader1.Read
>
> cmdCommand2 = New SqlCommand("Select * from Table2 Where Field1="
> &
> dataReader1("Field1"), cnn)
> dataReader2 = cmdCommand2.ExecuteReader
>
> While drCandidatos.Read
>
> End While
> dataReader1.Close()
> Loop
> cnn.Close()
>
> When i execute the program show this error:
> An unhandled exception of type 'System.InvalidOperationException'
> occurred
> in system.data.dll
> Additional information: There is already an open DataReader associated
> with
> this Connection which must be closed first.
>
> Are there other solution for that?
>
> Thanks for your help.
>

Dec 19 '05 #5

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

Similar topics

6
by: Ravikanth[MVP] | last post by:
Hi Alternative is check before assigning as TextBox value. TextBox1.Text =myRow==DBNull.Value)?"": (string)myRow HTH Ravikanth
7
by: Guadala Harry | last post by:
I'm trying to design all of my data access logic into one centralized assembly. I'm wondering how to implement DataReaders. There's plenty of documentation on passing DataSets to the client from...
5
by: Ryan Ternier | last post by:
I know how this should be done in regards to nTier, but it seems a bit inneficient, and was wondering if there's a solution that I havn't thought of yet. (I'm switching this loop to For Each Row...
0
by: Craig Graham | last post by:
I have an application that occasionally throws up an "open datareader associated with this connection". Obviously there is a path that leads to a datareader being left hanging; however finding it...
18
by: dbahooker | last post by:
team i'm having a tough time getting these data readers to work correctly I'd just like to be able to centralize my GetDataReader functions; so i can pass a simple SQL statement and be passed...
10
by: Doug | last post by:
If I am grabbing a lot of rows (say 10,000 from SQL) would I be helping or hurting the network by using a DataReader? I would think helping because it's really only returning 1 row at a time,...
4
by: Mike | last post by:
Assume i have a db connection and a datareader for that connection. Now I perform a query that retrieves 100MB of data. I know that the data reader gets 1 row at a time. However, there is a...
2
by: | last post by:
I am using a datareader to cycle through a list of records one at a time. I frequently get connection timeouts and am wondering what I am doing wrong. In more detail, I retrieve an excel...
0
by: gjok | last post by:
Hi. I have a few questions and I am hoping some kind programmer out there can offer me some guidance. I am trying to understand database interaction in .NET, and I'm having a really hard time as...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.