Error: "There is already an open DataReader associated with this Connection ....." (VB

I am using VB.NET 2003. When I try to execute following code, I get following error at the OleDBCommand given inside the While

Loop:

"There is already an open DataReader associated with this Connection which must be closed first".
-- CODE -
Private Sub PrepareSingleLedger(ByVal myFirmID As Long, ByVal myDate1 As Date, ByVal myDate2 As Date, ByVal myParty

As String)
Dim qSL As String = "Select PDate, FirstParty, ItemName, Rate, Qty, SecondParty, Commission, Remark, TranID From

PParty_Transac Where FirstParty = '" & myParty & "' or SecondParty = '" & myParty & "' and FirmID = " & myFirmID & " and PDate

between #" & Format(myDate1, "dd-MMM-yyyy") & "# and #" & Format(myDate2, "dd-MMM-yyyy") & "# Order By PDate"

Dim cmdSL As New OleDb.OleDbCommand(qSL, CN)
Dim drList As OleDb.OleDbDataReader

drList = cmdSL.ExecuteReader

While drList.Read = True
If IsDBNull(drList(1)) = False Then
If drList(1) = myParty Then
Dim qP1 As String = "Insert into tempSingleLedger (partyname) Values(drlist(1))" ' " & drList(0) & ",'S'," & drList(5) & "," &

drList(2) & "," & drList(3) & "," & drList(4) & "," & drList(6) & "," & drList(7)
Dim ins As New OleDb.OleDbCommand(qP1, CN)
ins.ExecuteNonQuery()
End If

End If
End While

drList.Close()

End Sub
-- CODE --
Here is description of what I want to do with the above code:
(1) qSL query is executed and its result is stored in the DataReader "drList".
(2) Now in the While Loop I want to check whether the FirstParty in qSL query = myParty. If that is the case the Insert Query written is to

be executed. "myParty" is the name of the Firm that is passed as argument to this routine.

[4251 byte] By [K.Rohit] at [2007-12-22]
# 1
You need to open your connection before you can executereader an oledbcommand. Close the connection after you close the datareader.
KenTucker at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
I have opened a Public Connection in a module and using it for this particular Form. Will it not work in this case?
K.Rohit at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
You can only have an connection associated with one datareader at a time according. The error is saying you are trying to open a second data reader on the same connection. So you are running into an issue where more than one of your subroutines is running at once or there is another datareader open in your code.

You can have more than one connection to the database at a time. I would create a new connection, open the datareader, use the data, close the datareader, and finally close the connection.

KenTucker at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Since you stated that you were working with 2003, Ken is right that you can only have one open datareader on a connection. You can have multiples if you have multiple connections. Often the cleanest solution is to find some way to deal with nested readers other than multiple active readers at the same time.

That being said, ADO.Net 2.0 and SQL 2005 added the ability to have "Multiple Active ResultSets" (or MARS). See http://msdn2.microsoft.com/en-us/library/h32h3abf.aspx for more information on this option.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx

jwooley at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...