JT4U
JT4U

Reputation: 620

Receiving Err Msg: There is already an open DataReader associated with this Command which must be closed first

        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Name\Desktop\AccessFile.accdb;"
        con.Open()
        Dim strQ As String = "select * from Table1 where Ticket_No = '" & Text_TicketNo.Text.ToString & "';".ToString
        Dim cmdQ As OleDbCommand = New OleDbCommand(strQ, con)
        Dim QReader As OleDbDataReader
        Dim it As String
        QReader = cmdQ.ExecuteReader
        If QReader.HasRows Then
            While QReader.Read
                it = QReader.Item("user_name")
                MsgBox(it)
            End While
        End If
        cmdQ.ExecuteNonQuery()
        con.Close()

Receiving the following error above. Not sure where this connection closure needs to take place. Thanks in advance for the help. Tried to look for this online but couldn't find any help for this issue. Tried using my eyes but i think i just need another person to look at my code and tell me where the problem is. Thanks.

The error is occuring on the following line. cmdQ.ExecuteNonQuery()

Upvotes: 1

Views: 28

Answers (1)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391336

The problem is the extra call here:

cmdQ.ExecuteNonQuery()

You never need this unless you're issuing a UPDATE, DELETE, INSERT, or any other of the statements that modify stuff. The SELECT query statement is only used with ExecuteReader.

So remove that line and the exception should go away.

Upvotes: 2

Related Questions