Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > .NET Web Services
|
.NET Web Services Discussions about .NET XML Web Service technologies including ASMX files, WSDL and SOAP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Web Services section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 8th, 2004, 12:08 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default executing 2 datareaders



i want to excute 2 queries ,second query will take the firrst querys output as parameter.
so i am using two SqlDataReader,sqlcommand.
when i try to execute second reader its saying below error

Error

rsroles.ExecuteReader Run-time exception thrown : System.InvalidOperationException - There is already an open DataReader associated with this Connection which must be closed first.

if i close and open dbconnection its fine but i am looping the first query so while looping here (myReadermain.GetString(0)) its saying error.

Is there any otherway to query inner sql means othere than datareader can i use any othe object?

here i am placing my code

thanks.




        Dim myReadermain, myReader As SqlDataReader
            Dim mainrs, rsroles As SqlCommand
        Dim db As New SqlConnection(connstr)

        sql = "select distinct x from y "
            mainrs = New SqlCommand(sql, db)
            myReadermain = mainrs.ExecuteReader
            Try
                While myReadermain.Read()
                    sql = "select name from b where z=" & Trim(myReadermain.GetString(0))
                    rsroles = New SqlCommand(sql, db)
                    myReader = rsroles.ExecuteReader
                    While myReader.Read()
                        If Trim(username) = Trim(myReader.GetString(0)) Then
                            rolesstring = rolesstring & Trim(myReadermain.GetString(0)) & "','"
                            Exit While
                        End If
                    End While
                    rsroles.Dispose()
                   myReader.Close()
                End While
            Finally
                myReadermain.Close()
                mainrs.Dispose()
            End Try



 
Old January 8th, 2004, 01:46 AM
Registered User
 
Join Date: Jan 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
   A datareader is a connected recordset. So your connection is not free to be used by other data objects. To acheive what you want you should use a DataSet or another way could be to move everything from the datareader to a collection object and close the datareader to free up the connection. Hope this helps.

Regards,
Gurjit

 
Old January 8th, 2004, 08:15 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Maybe there is a way to rework the query you are using so you don't need to use multiple readers. This would speed up the code be eliminating the multiple calls.

Can you provide a little more detail on what the two queries are doing together so we might be able to suggestion query alternatives?

Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
EXECUTING A STRING (HELP PLEASE!!) hossrad SQL Server 2000 1 May 15th, 2007 07:37 AM
DataReaders Open Problem gaurav_jain2403 ADO.NET 3 July 10th, 2006 05:38 AM
2 DataReaders Open Problem gaurav_jain2403 ASP.NET 1.0 and 1.1 Professional 1 July 2nd, 2006 10:05 PM
Multiple Datareaders using the same connection malarvizhi C# 0 June 20th, 2006 06:28 AM
Two Datareaders?? hexOffender VB Databases Basics 0 March 22nd, 2006 04:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.