473,888 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

already an open DataReader associated with this Command which must be closed first

Hi,

i get the error:
"There is already an open DataReader associated with this Command which must
be closed first"

Thanks
Bart
-----------------------------------------
Imports System.Data.sql client
Imports System.Data
....
....
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteSca lar

sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteRea der

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetVal ue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNon Query()
Next

dtreader.Close( )
---------------------------------------------------------------
Mar 27 '07 #1
13 3424
Create another command object for running the insert:

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNo nQuery()
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Bart" <b@sdq.dcwrot e in message
news:uk******** ******@TK2MSFTN GP02.phx.gbl...
Hi,

i get the error:
"There is already an open DataReader associated with this Command which
must
be closed first"

Thanks
Bart
-----------------------------------------
Imports System.Data.sql client
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteSca lar

sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteRea der

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetVal ue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNon Query()
Next

dtreader.Close( )
---------------------------------------------------------------


Mar 27 '07 #2
Hi, thansk for replying

I did this:

sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteSca lar

sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteRea der

For j = 1 To recpc
dtreader.Read()
ww = dtreader.GetVal ue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNo nQuery()
Next
dtreader.Close( )

but still same error at line: comd1.ExecuteNo nQuery()
"Eliyahu Goldin" <RE************ **************@ mMvVpPsS.orgsch reef in
bericht news:OB******** *****@TK2MSFTNG P02.phx.gbl...
Create another command object for running the insert:

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNo nQuery()
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin

Mar 27 '07 #3
You need to re-structure the code to close the first datareader before
starting using another one on the same connection. Only one request can be
served at the time and untill the first reader closes it won't let anyone
else to run.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Bart" <b@sdq.dcwrot e in message
news:Og******** ******@TK2MSFTN GP02.phx.gbl...
Hi, thansk for replying

I did this:

sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteSca lar

sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteRea der

For j = 1 To recpc
dtreader.Read()
ww = dtreader.GetVal ue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNo nQuery()
Next
dtreader.Close( )

but still same error at line: comd1.ExecuteNo nQuery()
"Eliyahu Goldin" <RE************ **************@ mMvVpPsS.orgsch reef in
bericht news:OB******** *****@TK2MSFTNG P02.phx.gbl...
Create another command object for running the insert:

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNo nQuery()
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin



Mar 27 '07 #4
Bart wrote:
Hi,

i get the error:
"There is already an open DataReader associated with this Command which must
be closed first"

Thanks
Bart
-----------------------------------------
Imports System.Data.sql client
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteSca lar

sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteRea der

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetVal ue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNon Query()
Next

dtreader.Close( )
---------------------------------------------------------------

You can't use the connection for running queries while it's used by the
data reader.

Why don't you do this in a single query?

"insert into mytable (field1) select pcnt from pc where lokl='" & w & "';"

--
Göran Andersson
_____
http://www.guffa.com
Mar 27 '07 #5
Not sure why you want to loop to do this, as this can all be done on the SQL
side, with a temp table, unless you are persisting the answers for a long
time. Even with a permanent table, you can clear the table before rerunning
this operation. If you need to know which number the record is, you can use
an IDENTITY field, in SQL Server (have to work from DUAL in Oracle) or
autonumber in Access.

This is the proc in SQL Server:

CREATE PROCEDURE TestProc
(
@w int -- Not sure of type here
)
AS

INSERT INTO MyTable (pcnr)
SELECT pcnr FROM pc
WHERE lokl = @w

SELECT @@COUNT

You now can use the return value for count and run the insert on all records
at the same time. The syntax is much easier.

1. Set up connection
2. Set up command
3. Set command as CommandType.Sto redProcedure
4. Run ExecuteScalar on Command and retrieve count of records changed

Very effficient and much easier to manage.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** ***************
Think outside the box!
*************** *************** ***************
"Bart" <b@sdq.dcwrot e in message
news:uk******** ******@TK2MSFTN GP02.phx.gbl...
Hi,

i get the error:
"There is already an open DataReader associated with this Command which
must be closed first"

Thanks
Bart
-----------------------------------------
Imports System.Data.sql client
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteSca lar

sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteRea der

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetVal ue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNon Query()
Next

dtreader.Close( )
---------------------------------------------------------------

Mar 27 '07 #6
Good morning Bart,

You used the same connection for more than one data readers and I guess
you're not using MARS - Multiple Active Result Sets (which is supported by
SQL Server 2005 only), therefore, once reader is executed, connection is
assigned to it exclusively - to release the connection, you have to close
connection or reader. In addition to that, there is not point to make things
complex and inefficient (shooting database many times in the loop as you’ve
presented in the posted code snipped). It’s very easy to build the query that
does exactly the same thing, but in one roundtrip:

sql = "INSERT INTO mytable (field1) SELECT pcnr FROM pc WHERE lokl = @lokl"

comd = new SqlCommand(sql, oConnection)
comd.Parameters .Add("@lokl", SqlDbType.NVarC har).Value = w

try
oConnection.Ope n()
comd.ExecuteNon Query();
catch ex as Exception
throw ex
finally
oConnection.Clo se()
end try

Done. Hope this helps
--
Milosz
"Bart" wrote:
Hi, thansk for replying

I did this:

sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteSca lar

sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteRea der

For j = 1 To recpc
dtreader.Read()
ww = dtreader.GetVal ue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNo nQuery()
Next
dtreader.Close( )

but still same error at line: comd1.ExecuteNo nQuery()
"Eliyahu Goldin" <RE************ **************@ mMvVpPsS.orgsch reef in
bericht news:OB******** *****@TK2MSFTNG P02.phx.gbl...
Create another command object for running the insert:

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNo nQuery()
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin



Mar 28 '07 #7
Thanks to all ...

But in fact, it's not so simple, because there are other parameters needed.
What i try to do is:

sql = "INSERT INTO studres (logon, dag, uur, type, pcnr) SELECT [lol] ,
[dag], [uur],'l', pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
comd.Parameters .AddWithValue("[lol]", lol)
comd.Parameters .AddWithValue("[dag]", tda)
comd.Parameters .AddWithValue("[uur]", vv)
comd.ExecuteNon Query()

but this gives the error:"Incorrec t syntax near 'nvarchar'.

In sql server, field login is nvarchar(25), field dag is Datetime and field
uur is int

....
"Cowboy (Gregory A. Beamer)" <No************ @comcast.netNoS pamMschreef in
bericht news:FB******** *************** ***********@mic rosoft.com...
Not sure why you want to loop to do this, as this can all be done on the
SQL side, with a temp table, unless you are persisting the answers for a
long time. Even with a permanent table, you can clear the table before
rerunning this operation. If you need to know which number the record is,
you can use an IDENTITY field, in SQL Server (have to work from DUAL in
Oracle) or autonumber in Access.

This is the proc in SQL Server:

CREATE PROCEDURE TestProc
(
@w int -- Not sure of type here
)
AS

INSERT INTO MyTable (pcnr)
SELECT pcnr FROM pc
WHERE lokl = @w

SELECT @@COUNT

You now can use the return value for count and run the insert on all
records at the same time. The syntax is much easier.

1. Set up connection
2. Set up command
3. Set command as CommandType.Sto redProcedure
4. Run ExecuteScalar on Command and retrieve count of records changed

Very effficient and much easier to manage.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** ***************
Think outside the box!
*************** *************** ***************
"Bart" <b@sdq.dcwrot e in message
news:uk******** ******@TK2MSFTN GP02.phx.gbl...
>Hi,

i get the error:
"There is already an open DataReader associated with this Command which
must be closed first"

Thanks
Bart
-----------------------------------------
Imports System.Data.sql client
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteSca lar

sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteRea der

for j = 1 To recpc
dtreader.Read( )
ww = dtreader.GetVal ue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNo nQuery()
Next

dtreader.Close ()
---------------------------------------------------------------


Mar 28 '07 #8
Specify the data type for the parameters.
Example:

comd.Parameters .Add("@lol", SqlDbType.NVarC har. 25).Value = lol

Parameters are named using @. Square brackets are used to specify that a
string is a name. It's used for names with non-alphanumeric characters,
like [current-price], or names that are keywords, like [from].

Bart wrote:
Thanks to all ...

But in fact, it's not so simple, because there are other parameters needed.
What i try to do is:

sql = "INSERT INTO studres (logon, dag, uur, type, pcnr) SELECT [lol] ,
[dag], [uur],'l', pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
comd.Parameters .AddWithValue("[lol]", lol)
comd.Parameters .AddWithValue("[dag]", tda)
comd.Parameters .AddWithValue("[uur]", vv)
comd.ExecuteNon Query()

but this gives the error:"Incorrec t syntax near 'nvarchar'.

In sql server, field login is nvarchar(25), field dag is Datetime and field
uur is int

...
"Cowboy (Gregory A. Beamer)" <No************ @comcast.netNoS pamMschreef in
bericht news:FB******** *************** ***********@mic rosoft.com...
>Not sure why you want to loop to do this, as this can all be done on the
SQL side, with a temp table, unless you are persisting the answers for a
long time. Even with a permanent table, you can clear the table before
rerunning this operation. If you need to know which number the record is,
you can use an IDENTITY field, in SQL Server (have to work from DUAL in
Oracle) or autonumber in Access.

This is the proc in SQL Server:

CREATE PROCEDURE TestProc
(
@w int -- Not sure of type here
)
AS

INSERT INTO MyTable (pcnr)
SELECT pcnr FROM pc
WHERE lokl = @w

SELECT @@COUNT

You now can use the return value for count and run the insert on all
records at the same time. The syntax is much easier.

1. Set up connection
2. Set up command
3. Set command as CommandType.Sto redProcedure
4. Run ExecuteScalar on Command and retrieve count of records changed

Very effficient and much easier to manage.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************** *************** *************** *
Think outside the box!
************** *************** *************** *
"Bart" <b@sdq.dcwrot e in message
news:uk******* *******@TK2MSFT NGP02.phx.gbl.. .
>>Hi,

i get the error:
"There is already an open DataReader associated with this Command which
must be closed first"

Thanks
Bart
-----------------------------------------
Imports System.Data.sql client
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteSca lar

sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteRea der

for j = 1 To recpc
dtreader.Read ()
ww = dtreader.GetVal ue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteN onQuery()
Next

dtreader.Clos e()
---------------------------------------------------------------



--
Göran Andersson
_____
http://www.guffa.com
Mar 28 '07 #9
Yes, that's it
thanks

"Göran Andersson" <gu***@guffa.co mschreef in bericht
news:eZ******** ******@TK2MSFTN GP02.phx.gbl...
Specify the data type for the parameters.
Example:

comd.Parameters .Add("@lol", SqlDbType.NVarC har. 25).Value = lol

Parameters are named using @. Square brackets are used to specify that a
string is a name. It's used for names with non-alphanumeric characters,
like [current-price], or names that are keywords, like [from].

Bart wrote:
>Thanks to all ...

But in fact, it's not so simple, because there are other parameters
needed. What i try to do is:

sql = "INSERT INTO studres (logon, dag, uur, type, pcnr) SELECT [lol] ,
[dag], [uur],'l', pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
comd.Parameters .AddWithValue("[lol]", lol)
comd.Parameters .AddWithValue("[dag]", tda)
comd.Parameters .AddWithValue("[uur]", vv)
comd.ExecuteNon Query()

but this gives the error:"Incorrec t syntax near 'nvarchar'.

In sql server, field login is nvarchar(25), field dag is Datetime and
field uur is int

...
"Cowboy (Gregory A. Beamer)" <No************ @comcast.netNoS pamMschreef
in bericht news:FB******** *************** ***********@mic rosoft.com...
>>Not sure why you want to loop to do this, as this can all be done on the
SQL side, with a temp table, unless you are persisting the answers for a
long time. Even with a permanent table, you can clear the table before
rerunning this operation. If you need to know which number the record
is, you can use an IDENTITY field, in SQL Server (have to work from DUAL
in Oracle) or autonumber in Access.

This is the proc in SQL Server:

CREATE PROCEDURE TestProc
(
@w int -- Not sure of type here
)
AS

INSERT INTO MyTable (pcnr)
SELECT pcnr FROM pc
WHERE lokl = @w

SELECT @@COUNT

You now can use the return value for count and run the insert on all
records at the same time. The syntax is much easier.

1. Set up connection
2. Set up command
3. Set command as CommandType.Sto redProcedure
4. Run ExecuteScalar on Command and retrieve count of records changed

Very effficient and much easier to manage.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************* *************** *************** **
Think outside the box!
************* *************** *************** **
"Bart" <b@sdq.dcwrot e in message
news:uk****** ********@TK2MSF TNGP02.phx.gbl. ..
Hi,

i get the error:
"There is already an open DataReader associated with this Command which
must be closed first"

Thanks
Bart
-----------------------------------------
Imports System.Data.sql client
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteSca lar

sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteRea der

for j = 1 To recpc
dtreader.Rea d()
ww = dtreader.GetVal ue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.Execute NonQuery()
Next

dtreader.Clo se()
---------------------------------------------------------------




--
Göran Andersson
_____
http://www.guffa.com

Mar 28 '07 #10

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

Similar topics

20
7226
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I get the error "There is already an open DataReader associated with this Connection which must be closed first." How can I fix this error ? For each DataReader, do I want to open and close the connection (in this case adoCon) to avoid this error ?...
10
6118
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the code below which when executed generates the following error message: 'There is already an open datareader with this command which must be closed first' Private Sub MainMenu_Load(ByVal sender As System.Object, ByVal e As
13
31707
by: Bart | last post by:
Hi, i get the error: "There is already an open DataReader associated with this Command which must be closed first" Thanks Bart ----------------------------------------- Imports System.Data.sqlclient
3
13204
by: BLUE | last post by:
I've a TransactionScope in which I select some data and I want to do some queries for each record retrieved with my select. I'm using a DataReader and for each record I do factory.CreateCommand() and then I execute the command, but I get the following exception message: " There is already an open DataReader associated with this Command which must be closed first. " Searching on google I've found I've to create another connection, but...
0
9961
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
11178
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...
0
10777
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10438
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
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
7148
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();...
1
4642
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
4243
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.