excuting 2 datareaders

Results 1 to 2 of 2

Thread: excuting 2 datareaders

  1. #1
    Join Date
    Dec 1969

    Default excuting 2 datareaders

    &nbsp;<BR> <BR>i want to excute 2 queries ,second query will take the firrst querys output as parameter.<BR>so i am using two SqlDataReader,sqlcommand.<BR>when i try to execute second reader its saying below error<BR><BR>Error<BR><BR>rsroles.ExecuteReader Run-time exception thrown : System.InvalidOperationException - There is already an open DataReader associated with this Connection which must be closed first. <BR><BR>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.<BR><BR>Is there any otherway to query inner sql means othere than datareader can i use any othe object?<BR><BR>here i am placing my code<BR><BR>thanks.<BR><BR><BR><BR><BR> Dim myReadermain, myReader As SqlDataReader<BR> Dim mainrs, rsroles As SqlCommand<BR> Dim db As New SqlConnection(connstr)<BR><BR> sql = "select distinct x from y "<BR> mainrs = New SqlCommand(sql, db)<BR> myReadermain = mainrs.ExecuteReader<BR> Try<BR> While myReadermain.Read()<BR> sql = "select name from b where z=" & Trim(myReadermain.GetString(0))<BR> rsroles = New SqlCommand(sql, db)<BR> myReader = rsroles.ExecuteReader<BR> While myReader.Read()<BR> If Trim(username) = Trim(myReader.GetString(0)) Then<BR> rolesstring = rolesstring & Trim(myReadermain.GetString(0)) & "&#039;,&#039;"<BR> Exit While<BR> End If<BR> End While<BR> rsroles.Dispose()<BR> myReader.Close()<BR> End While<BR> Finally<BR> myReadermain.Close()<BR> mainrs.Dispose()<BR> End Try<BR> <BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: excuting 2 datareaders

    DataReaders use the connection exclusively, so you&#039;ll need to either establish two connections, or read the data in the first reader into an array or collection, close that reader then loop through the data.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts