Tuesday, March 20, 2012

Problems with more than 1 reader

Dim connAs SqlConnection =Nothing

Dim cmdAs SqlCommand

Dim cmd2As SqlCommand

try

conn =New SqlConnection(ConfigurationManager.ConnectionStrings("myConnection").ConnectionString)

conn.Open()

cmd =New SqlCommand

cmd.Connection = conn

cmd2 =New SqlCommand

cmd2.Connection = conn

dim readerAs SqlDataReader = cmd.ExecuteReader

Right this works and I can access reader fine

but as soon as I do the following I get an error

reader2 = cmd2.ExecuteReader (reader2 is already declared just not put on here)

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

which is odd because there sqlcommand used for reader2 is different.

Can I not use more than one reader at anyone time? I used to have loads of recordsets open in asp 3 no prob.

Cheers

Hi,

You should close first datareader just before you execute second reader.

Then it will work fine. Because there is only one connection and the connection is already used by first reader.

/Bindu

|||

try setting your 2nd command object to its own connection.

cmd2.Connection =New SqlConnection(ConfigurationManager.ConnectionStrings("myConnection").ConnectionString)

|||

thanks guys I'll try them out.

I ended up putting the results into collections and then looped them instead.

No comments:

Post a Comment