Following is a statement from SqlDataReader Class
While the SqlDataReader is being used, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it.
I have code that has one open connection used by two different commands. There are actually two methods. The second method is getting called in the while(reader.Read())
loop.
I get intermittent errors saying – There is already an open DataReader associated with this Command which must be closed first
.
But why isn’t it always throwing error?
REFERENCES
- Default Result Set Processing and Multiple Active Result Sets
- SQL Server Connection Basics
- Regarding a small confusion about DataReader