How to use two nested datareaders ?

Results 1 to 2 of 2

Thread: How to use two nested datareaders ?

  1. #1
    Join Date
    Dec 1969

    Default How to use two nested datareaders ?

    I just want to go through a datareader (datareader1) and as I read I want to run another store procedure using another datareader (datareader2) using some of the data from datareader1.<BR><BR>I am getting the error:"There is already an open DataReader associated with this Connection which must be closed first"<BR> and I understand why I am getting this error but I don&#039;t know how to do it another way. Do you ? Help !!!<BR><BR>example:<BR><BR>&#060;code&#062;<BR>Dim order_id as Integer = 1254<BR><BR>Dim myDataReader1 As SqlDataReader<BR>Dim myDataReader2 As SqlDataReader<BR><BR>Dim myConnection As SqlConnection = New SqlConnection...<BR><BR>Dim myCommand1 As SqlCommand = New SqlCommand("s_sp1", myConnection)<BR><BR>myCommand1.CommandType = CommandType.StoredProcedure<BR><BR>myCommand1.Para meters.Add("@Order_id", order_id)<BR>Try<BR> myConnection.Open()<BR><BR> myDataReader1 = myCommand1.ExecuteReader()<BR><BR> While myDataReader1.Read()<BR> ....<BR> Cat_Id = myDataReader1("category_id")<BR> <BR> Dim myCommand2 As SqlCommand = New SqlCommand("s_sp2", myConnection)<BR><BR> myCommand1.Parameters.Add("@cat_id", cat_id)<BR><BR> myDataReader2 = myCommand2.ExecuteReader()<BR><BR> if myDataReader2.Read Then<BR> ...<BR> end if<BR> myDataReader2.Close<BR> End While<BR><BR>Catch sql_exce As SqlException<BR>...<BR>Finally<BR> myConnection.Close()<BR> myDataReader1.Close<BR>End Try<BR>&#060;/code&#062;<BR><BR> <BR>

  2. #2

    Default RE: How to use two nested datareaders ?

    You could use datasets instead of readers. No persistent connection, no more problem.<BR><BR>Check the ADO.Net thread at the bottom of the page, the "new link" message links to a detail discussion of datareaders.

Posting Permissions

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