1

I'm trying to refresh data within my EntityFramework ObjectSet but I'm receiving the following error: There is already an open DataReader associated with this Command which must be closed first.

So I have a ObjectContext which returns me an ObjectSet of customers. That list of customers I then stick into a grid. Below is a snippet from my method:

public class CustomerEntity : SECValidation.SECEntity
{
    ObjectSet<Customer> objectCustomer;

    public override void InitialiseObject()
    {
        objectCustomer = myObjectContext.CreateObjectSet<Customer>("Customers");
        objectCustomer.MergeOption = MergeOption.OverwriteChanges;
    }

    public override List<T> SelectRecords<T>()
    {
       return (from custDetails in objectCustomer.Include("Owner_Lookup")
           .Include("Business_Type_Lookup").Include("Assets").Include("Client")
           select custDetails).Cast<T>().ToList();
    }

        public override T SelectRecordByUNID<T>(int UN_ID)
        {
            return (from custDetails in objectCustomer.Include("Owner_Lookup").Include("Business_Type_Lookup").Include("Assets").Include("Client")
                    where custDetails.UN_Customer == UN_ID
                    select custDetails).Cast<T>().SingleOrDefault();
        }
}

So I create an instance of my CustomerEntity class and call InitialiseObject(). After that I call SelectRecords() which provides the results for my grid.

I have code on the SelectedIndex changed event on the grid which calls the method SelectRecordByUNID passing in the customerID of the selected row. This selects a customer record which is used elsewhere.

Now if somebody updates the data elsewhere (another instance of the app) I would like to refresh my objectCustomer to see those changes.

However the SelectRecords method runs OK, but when the SelectRecordByUNID method is called it fails with the error I mentioned above. The error says that there is an associated DataReader already open with this command and to close it. How can I get rounds this without closing the Data Reader as other data is opened which is related to the datasource of the customer grid?

I use myObjectContext to create object set for different entities too.

I'm using c# and .Net4.0

Help me please and I hope that makes sense!?!

Thanks.

user1131661
  • 209
  • 3
  • 8
  • 18
  • possible duplicate of [Entity Framework: There is already an open DataReader associated with this Command](http://stackoverflow.com/questions/4867602/entity-framework-there-is-already-an-open-datareader-associated-with-this-comma) – Ladislav Mrnka Feb 20 '12 at 15:25
  • Not sure if it is or not to be honest. I've tried setting MultipleActiveResultSets=True and that makes no difference. Thanks for your reply – user1131661 Feb 20 '12 at 16:12
  • In such case you must provide more information in your question because you are probably doing something wrong. – Ladislav Mrnka Feb 20 '12 at 16:14
  • OK, I've updated the question if that helps. Let me know if not. – user1131661 Feb 20 '12 at 16:26

1 Answers1

0

It is strange error because your query is executed by ToList and DataReader should be closed.

Try to not use Refresh in this case. You are executing query anyway so try this instead:

public override List<T> SelectRecords<T>()
{
   objectCustomer.MergeOption = MergeOption.OverwriteChanges;
   return (from custDetails in objectCustomer.Include("Owner_Lookup")
       .Include("Business_Type_Lookup").Include("Assets").Include("Client")
       select custDetails).ToList();
}
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • Thanks for the reply. That works for my customer grid now but it seems to have pushed the error else where. I've updated the job to reflect this. Thanks again – user1131661 Feb 21 '12 at 09:59