469,920 Members | 2,239 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,920 developers. It's quick & easy.

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.sqlclient
Imports System.Data
....
....
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

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

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
Next

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

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNonQuery()
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Bart" <b@sdq.dcwrote in message
news:uk**************@TK2MSFTNGP02.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.sqlclient
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

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

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
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.ExecuteScalar

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

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

but still same error at line: comd1.ExecuteNonQuery()
"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgschree f in
bericht news:OB*************@TK2MSFTNGP02.phx.gbl...
Create another command object for running the insert:

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNonQuery()
--
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.dcwrote in message
news:Og**************@TK2MSFTNGP02.phx.gbl...
Hi, thansk for replying

I did this:

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

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

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

but still same error at line: comd1.ExecuteNonQuery()
"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgschree f in
bericht news:OB*************@TK2MSFTNGP02.phx.gbl...
Create another command object for running the insert:

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNonQuery()
--
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.sqlclient
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

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

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
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.StoredProcedure
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.dcwrote in message
news:uk**************@TK2MSFTNGP02.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.sqlclient
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

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

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
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.NVarChar).Value = w

try
oConnection.Open()
comd.ExecuteNonQuery();
catch ex as Exception
throw ex
finally
oConnection.Close()
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.ExecuteScalar

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

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

but still same error at line: comd1.ExecuteNonQuery()
"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgschree f in
bericht news:OB*************@TK2MSFTNGP02.phx.gbl...
Create another command object for running the insert:

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = New SqlCommand(sql, oConnection)
comd1.ExecuteNonQuery()
--
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.ExecuteNonQuery()

but this gives the error:"Incorrect 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.netNoSpamMschreef in
bericht news:FB**********************************@microsof t.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.StoredProcedure
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.dcwrote in message
news:uk**************@TK2MSFTNGP02.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.sqlclient
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

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

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
Next

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


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

comd.Parameters.Add("@lol", SqlDbType.NVarChar. 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.ExecuteNonQuery()

but this gives the error:"Incorrect 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.netNoSpamMschreef in
bericht news:FB**********************************@microsof t.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.StoredProcedure
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.dcwrote in message
news:uk**************@TK2MSFTNGP02.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.sqlclient
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

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

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
Next

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



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

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

comd.Parameters.Add("@lol", SqlDbType.NVarChar. 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.ExecuteNonQuery()

but this gives the error:"Incorrect 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.netNoSpamMschreef
in bericht news:FB**********************************@microsof t.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.StoredProcedure
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.dcwrote in message
news:uk**************@TK2MSFTNGP02.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.sqlclient
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

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

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
Next

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




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

Mar 28 '07 #10
Goran,

would it be possible to do this::

sql = "INSERT INTO studres ( uur, pcnr) SELECT uur from uur where range='"
& v & "', pcnr FROM pc where naam='" & w & "';"

Thanks

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

comd.Parameters.Add("@lol", SqlDbType.NVarChar. 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.ExecuteNonQuery()

but this gives the error:"Incorrect 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.netNoSpamMschreef
in bericht news:FB**********************************@microsof t.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.StoredProcedure
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.dcwrote in message
news:uk**************@TK2MSFTNGP02.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.sqlclient
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

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

for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
Next

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




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

Mar 28 '07 #11
Join the tables:

insert into studres (uur, pcnr)
select uur.uur, pc.pcnr
from uur
inner join pc on pc.naam = @w
where uur.range = @v
Bart wrote:
Goran,

would it be possible to do this::

sql = "INSERT INTO studres ( uur, pcnr) SELECT uur from uur where range='"
& v & "', pcnr FROM pc where naam='" & w & "';"

Thanks

"Göran Andersson" <gu***@guffa.comschreef in bericht
news:eZ**************@TK2MSFTNGP02.phx.gbl...
>Specify the data type for the parameters.
Example:

comd.Parameters.Add("@lol", SqlDbType.NVarChar. 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.ExecuteNonQuery()

but this gives the error:"Incorrect 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.netNoSpamMschreef
in bericht news:FB**********************************@microsof t.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.StoredProcedure
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.dcwrote in message
news:uk**************@TK2MSFTNGP02.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.sqlclient
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar
>
sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteReader
>
for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
Next
>
dtreader.Close()
---------------------------------------------------------------
>

--
Göran Andersson
_____
http://www.guffa.com
Mar 28 '07 #12
thanks,
I tried something else and it works too::

sql = "INSERT INTO studres (logon, dag, type, uur, pcnr) SELECT @lol , @dag,
'l', uur.uur, pc.pcnr FROM uur, pc WHERE pc.naam=@pcna AND uur.range= @uur;"


"Göran Andersson" <gu***@guffa.comschreef in bericht
news:eU**************@TK2MSFTNGP04.phx.gbl...
Join the tables:

insert into studres (uur, pcnr)
select uur.uur, pc.pcnr
from uur
inner join pc on pc.naam = @w
where uur.range = @v
Bart wrote:
>Goran,

would it be possible to do this::

sql = "INSERT INTO studres ( uur, pcnr) SELECT uur from uur where
range='" & v & "', pcnr FROM pc where naam='" & w & "';"

Thanks

"Göran Andersson" <gu***@guffa.comschreef in bericht
news:eZ**************@TK2MSFTNGP02.phx.gbl...
>>Specify the data type for the parameters.
Example:

comd.Parameters.Add("@lol", SqlDbType.NVarChar. 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.ExecuteNonQuery()

but this gives the error:"Incorrect 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.netNoSpamM>
schreef in bericht
news:FB**********************************@micro soft.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.StoredProcedure
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.dcwrote in message
news:uk**************@TK2MSFTNGP02.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.sqlclient
>Imports System.Data
>...
>...
>sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
>comd = New SqlCommand(sql, oConnection)
>recpc = comd.ExecuteScalar
>>
>sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
>comd = New SqlCommand(sql, oConnection)
>dtreader = comd.ExecuteReader
>>
>for j = 1 To recpc
>dtreader.Read()
>ww = dtreader.GetValue(0)
>sql = "INSERT INTO mytable (field1 values(" & ww & ")"
>comd.ExecuteNonQuery()
>Next
>>
>dtreader.Close()
>---------------------------------------------------------------
>>


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

Mar 28 '07 #13
Yes, that's how you would write a join before the join keyword was
introduced.

Bart wrote:
thanks,
I tried something else and it works too::

sql = "INSERT INTO studres (logon, dag, type, uur, pcnr) SELECT @lol , @dag,
'l', uur.uur, pc.pcnr FROM uur, pc WHERE pc.naam=@pcna AND uur.range= @uur;"


"Göran Andersson" <gu***@guffa.comschreef in bericht
news:eU**************@TK2MSFTNGP04.phx.gbl...
>Join the tables:

insert into studres (uur, pcnr)
select uur.uur, pc.pcnr
from uur
inner join pc on pc.naam = @w
where uur.range = @v
Bart wrote:
>>Goran,

would it be possible to do this::

sql = "INSERT INTO studres ( uur, pcnr) SELECT uur from uur where
range='" & v & "', pcnr FROM pc where naam='" & w & "';"

Thanks

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

comd.Parameters.Add("@lol", SqlDbType.NVarChar. 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.ExecuteNonQuery()
>
but this gives the error:"Incorrect 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.netNoSpamM>
schreef in bericht
news:FB**********************************@micr osoft.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.StoredProcedure
>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.dcwrote in message
>news:uk**************@TK2MSFTNGP02.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.sqlclient
>>Imports System.Data
>>...
>>...
>>sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
>>comd = New SqlCommand(sql, oConnection)
>>recpc = comd.ExecuteScalar
>>>
>>sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
>>comd = New SqlCommand(sql, oConnection)
>>dtreader = comd.ExecuteReader
>>>
>>for j = 1 To recpc
>>dtreader.Read()
>>ww = dtreader.GetValue(0)
>>sql = "INSERT INTO mytable (field1 values(" & ww & ")"
>>comd.ExecuteNonQuery()
>>Next
>>>
>>dtreader.Close()
>>---------------------------------------------------------------
>>>

--
Göran Andersson
_____
http://www.guffa.com
--
Göran Andersson
_____
http://www.guffa.com
Mar 28 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.