views:

324

answers:

3

I am getting the following error;

"There is already an open DataReader associated with this Command which must be closed first."

is it because I have used the reader in foreach loop ? or what the problem might be ? Regards BK

foreach( Apple a in listApple )
{


....
                   using (SmartSqlReader reader = Db.CurrentDb.ExecuteReader(sp))
                    {
                        while (reader.Read())
                        {
                            a.blablabla += reader.GetInt32("BLA_BLA_BLA"); 
                        }
                    }


.....

}
A: 

Add reader.Close() to close the SmartSqlReader

Scoregraphic
+1  A: 

Have you implemeneted the SmartSqlReader to close when it's disposed? The regular data readers implement the IDisposable interface and calls Close from the Dispose method.

If you don't close it properly it will keep the Command object occupied until the garbage collector will find the reader and clean it up.

Guffa
A: 

Try the following:

using (SmartSqlReader reader = Db.CurrentDb.ExecuteReader(sp))
 {
  while (reader.Read())
  {
   a.blablabla += reader.GetInt32("BLA_BLA_BLA"); 
  }
  reader.Close();
 }
Himadri
It's pointless to have the using block there if it doesn't do anything, or actually *worse* than pointless as it *looks* like it should do something...
Guffa