• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

Urgent! trying to get a recursive datareader to work

this grabs a number and a product, if the number and product exists, it processes it in get_row(), then it checks for a last job number, if it exists, calls itself again, otherwise it exists. Dont know what i'm doing wrong. I'm closing it, and I get the error message

Invalid attempt to Read when reader is closed.
happens on the line --->While r.Read()

    Public Shared Sub recursive(ByVal strVT_Number As String, ByVal strVT_Product As String)
        Dim strVT_Last As String
        Dim strVT_Prod As String
        Dim sqlCommtest As New SqlCommand("SELECT vt_job_number, vt_product, vt_last_job_number FROM vt_newsletters WHERE (vt_print_outside = 1) AND vt_product = '" & strVT_Product & "' and vt_job_number = " & strVT_Number, conRow)
        Dim r As SqlDataReader = sqlCommtest.ExecuteReader()
        While r.Read()
            Get_Row(r("vt_job_number"), r("vt_product"))
            If r("vt_last_job_number") <> "" Then
                strVT_Last = r("vt_last_job_number")
                strVT_Prod = r("vt_product")
                recursive(strVT_Last, strVT_Prod)
            End If
        End While
        DR = DSGrid.Tables(0).NewRow
    End Sub
  • 3
1 Solution
Raju SrivatsavayeSoftware EngineerCommented:
Do this:

Dim r As SqlDataReader = sqlCommtest.ExecuteReader(CommandBehavior.CloseConnection)
That should work
Raju SrivatsavayeSoftware EngineerCommented:
It might be Data.CommandBehavior.CloseConnection..Sometimes its this way..

Raju SrivatsavayeSoftware EngineerCommented:
Also you closed the reader in the while loop. Thats not correct.

Reader should be closed only aftet the while r.read loop.

if you want to exit the loop after else use exit while or something like that
Term56Author Commented:
none of the answers has helped, please cancel the open ticket

thank you.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now