Answered by:
DataReader.Read closed premeturely

Question
-
User-1293059278 posted
Hi
I noticed a very strange thing. I was debugging the following code, and thus had put the breakpoint at line: using (IDataReader dataReader = db.ExecuteReader(dbCommand)).
dataReader was getting populated, but after a halt of lets say 30 seconds, dataReader was emptied, while I was debugging.
So when control went to dataReader.Read(), i got error: "Invalid attempt to call FieldCount when reader is closed".
But if i execute the function at one go, without halting at dataReader line (without breakpoint), it works alright.
My question is what is triggering the dataReader to close ?
Its a legacy code, and have worked fine.
Database db;
db = DatabaseFactory.CreateDatabase();
db.AddInParameter(dbCommand, "ID", DbType.String, ID);
do{try{using (IDataReader dataReader = db.ExecuteReader(dbCommand)){while (dataReader.Read()){Product product = ReadProduct(dataReader);productList.Add(product);}}return productList;}DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand); db.AddInParameter(dbCommand, "ID", DbType.String, ID); using (IDataReader dataReader = db.ExecuteReader(dbCommand)) { while (dataReader.Read()) { Product product = ReadProduct(dataReader); productList.Add(product); } }
Thanks in advace
Tuesday, September 14, 2010 4:58 PM
Answers
-
User-311685349 posted
Yeah, I think so, just increase the timeout on the connection string and you shouldn't have any issues :)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 15, 2010 4:20 AM
All replies
-
User-311685349 posted
It sounds like it's timing out and releasing the connection... what happens if you let it run through
Tuesday, September 14, 2010 6:44 PM -
User-1293059278 posted
If I let it run continously, it works absolutely fine.
Its just the matter of halting the process for couple of seconds at data Reader.
Wanted to know if this is how it works ?
Thanks
Wednesday, September 15, 2010 4:14 AM -
User-311685349 posted
Yeah, I think so, just increase the timeout on the connection string and you shouldn't have any issues :)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 15, 2010 4:20 AM