in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

"DataReader already active on this command"?

Last post 03-05-2010 6:44 AM by langenscheidts. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 03-05-2010 6:31 AM

    "DataReader already active on this command"?

    Hello,

    I'm learning about VB.Net using the SQLite wrapper, and get this error message when calling SELECT while looping through a list of items. As shown, setting SQLRead=Nothing doesn't solve the run-time error:

    ------------------------

    Const STARTDIR = "C:\MyDir\"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            ListBox1.Items.Clear()

     

            Dim SQLconnect As New SQLite.SQLiteConnection()

            Dim SQLcommand As SQLite.SQLiteCommand

            Dim SQLtransaction As SQLite.SQLiteTransaction

            Dim SQLreader As SQLite.SQLiteDataReader

     

            SQLconnect.ConnectionString = "Data Source=test.sqlite;"

            SQLconnect.Open()

     

            SQLcommand = SQLconnect.CreateCommand

     

            Dim filelist As Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(STARTDIR, FileIO.SearchOption.SearchAllSubDirectories)

            Dim path As String

     

            For Each path In filelist

                'displayed twice

                ListBox1.Items.Add("before error")

                ListBox1.Refresh()

     

                SQLcommand.CommandText = "SELECT id,hash FROM files WHERE hash='@hash'"

                SQLcommand.Parameters.AddWithValue("@hash", "123456")

                'DataReader already active on this command

                SQLreader = SQLcommand.ExecuteReader()

                If Not SQLreader.HasRows Then

                    ListBox1.Items.Add("no row")

                Else

                    'Just ignore results

                    SQLreader = Nothing

                End If

            Next

    End Sub

    ------------------------

    Does someone know the cause/solution of this error?

    Thank you.

    PS : I had to copy/paste code from VB.Net Express to a WYSIWYG HTML editor to this forum. Does someone know how to post code so it's not turned into one-line garbage?

  • 03-05-2010 6:44 AM In reply to

    Re: "DataReader already active on this command"?

    Found it: For those interested, it seems like you have to call " SQLreader.Close()" to clear whatever data an SQLReader variable contains:

            For Each path In filelist
                'displayed twice
                ListBox1.Refresh()
    
                SQLcommand.CommandText = "SELECT id,hash FROM files WHERE hash='@hash'"
                SQLcommand.Parameters.AddWithValue("@hash", "123456")
                'DataReader already active on this command
                SQLreader = SQLcommand.ExecuteReader()
                If Not SQLreader.HasRows Then
                    ListBox1.Items.Add("no row")
                End If
                SQLreader.Close()
            Next
    
    My .15€
Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems