FAQ - Open DataReader and MARS

FAQ - Open DataReader and MARS

Old forum URL: forums.lhotka.net/forums/t/9223.aspx


tiago posted on Sunday, July 18, 2010

if you get the error message

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

this isn't a Csla problem. It's MARS related.

MARS = Multiple Active Record Sets and means exactly what it says: you can have more than one DataReader on a single connection. MARS isn't supported on every SQL database (for instance SQL2000 doesn't support it) and you must enavle it in the connection string (or equivalent).

I was using

protected void DataPortal_Fetch()
{
  using (SqlConnection cn = new SqlConnection(Database.DocStoreConnection))
  {
    using (SqlCommand cmd = new SqlCommand("GetDocStatusNVL", cn))
    {
      cmd.
CommandType = CommandType.StoredProcedure;
      cn.
Open();
      ...

 

 

and in order to take advantage of the ConnectionManager feature, changed it to

protected void DataPortal_Fetch()
{
 
using (var ctx = ConnectionManager<SqlConnection>.GetManagerDatabase.DocStoreConnection, false))
  {
   
using (SqlCommand cmd = new SqlCommand("GetDocClassNVL", ctx.Connection))
    {
      cmd.
CommandType = CommandType.StoredProcedure;
      ...

 

 

The difference is that on the later case, I'm reusing the connection (that's the purpose of ConnectionManager). Another difference is that I got the above error message. Sad

It looks like I'm in the middle of some other DataPortal operation and didn't close the DataReader. In fact I'm fetching a big read only list and converting the DocClass IDs to DocClass names. This is done on a row by row base. That's not a problem as the NVL is cached and the fecth only hits the database on the first call.

SOLUTION - Go back to the first form.

Copyright (c) Marimer LLC