Page 1 of 1

Issue with unclosed data reader

PostPosted: Tue Oct 27, 2015 10:46 am
by indikad
I appreciate if one of the developers can give me a hint where I can find a workaround or a fix for this issue.
In summary –
I have a plugin that acts on the sales quote window on save.
The event is - SalesQuote.SaveEnd

There is a lot of code - however the theory is simple. In this code I read the database in several different functions using a sql connection, command and a data reader.
Each function closes and disposes the data reader , command and the connection in its own finally block.

However :
After the second call to a certain function (getSalesQuoteLinesInventoryID) (that reads the database) – the next function call (to read the database) gives me the following error.
“there is already an open datareader associated with this command which must be closed”

As indicated above I close all objects. In addition I have spent a the whole day today trying to isolate any syntax errors that I cannot find.

Attached is the code if you could have a look. Attached also is the screenshot sequence of function calls and the last of which gives the error.

Re: Issue with unclosed data reader

PostPosted: Tue Oct 27, 2015 3:47 pm
by Mike.Sheen
indikad wrote:Attached is the code if you could have a look. Attached also is the screenshot sequence of function calls and the last of which gives the error.


You forgot the attachment.

Re: Issue with unclosed data reader  Topic is solved

PostPosted: Thu Oct 29, 2015 9:38 pm
by Mike.Sheen
The problem was resolved by the submitter, offline.

Turns out the problem was failure to use the using command to auto-dispose a SqlCommand.

You should always use using! i.e.:

Code: Select all
Using SQLCmd As SqlCommand = New SqlCommand(Sql, .SQLConnection, .SQLTransaction)
...
End Using