none
There is already an open DataReader associated with this Command which must be closed first in Entityframework RRS feed

  • Question

  • Hi,

    Here is my code, I want to update the flag & SaleseforceId of a request entity which has flag value as 0.

    Can anyone please explain what is wrong with the following code?

    public void SendPendingDataToSalesforce()
    {
                var db = new ZurshDBContext();            

                IQueryable<Zursh.Request> PendingRequestQuery = db.Requests.Where(r => r.SaleseForceFlag == "0");

                foreach (var request in PendingRequestQuery)
                {
                    demosdfc dm = new demosdfc();                
                    string message = dm.request(request, "save");
                    if (!message.Contains(' '))
                    {
                        request.SalesForceId = message;
                        request.SaleseForceFlag = "1";
                    }
                    else
                    {
                        request.SaleseForceFlag = "0";
                    }
                    db.Requests.Attach(request);
                    db.Entry(request).State = EntityState.Modified;
                    db.SaveChanges();
                }            
    }

    Monday, July 21, 2014 11:45 AM

Answers

  • Hello,

    From the WebMethod, it seems that this error is not related with it. Since I do not have your project environment, you could try to comment this code and run your code again to see whether the error is caused by this method.

    I notice that you initialize a context object without disposing it, please have a try with Using block as below to ensure the context is disposed:

                using (DFDBEntities db = new DFDBEntities())
    
                {
    
                    DFDBEntities db = new DFDBEntities();
    
                    IQueryable<Order> Orders = db.Orders.AsQueryable();
    
    
                    foreach (var order in Orders)
    
                    {
    
                        order.OrderName = "Changed";
    
                        db.Orders.Attach(order);
    
                        db.Entry(order).State = EntityState.Modified;
    
                        db.SaveChanges();
    
                    }
    
                }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 23, 2014 6:38 AM
    Moderator

All replies

  • Hello,

    >>Can anyone please explain what is wrong with the following code?

    Which line does it throw this error?  And what is the “demosdfc” object? Inside it, does it have any interop wity database? Because I just make a small test with your remained code, it could update value successfully:

    DFDBEntities db = new DFDBEntities();
    
    IQueryable<Order> Orders = db.Orders.AsQueryable();
    
    foreach (var order in Orders)
    
    {
    
                    order.OrderName = "1";
    
                    db.Orders.Attach(order);
    
                    db.Entry(order).State = EntityState.Modified;
    
                    db.SaveChanges();
    
    }
    

    So I am wondering the caused reason might be in the “demosdfc” object. Please share it with us. And you could also ensure this with removing code related the “demosdfc” object yourself. If it does not throw this error, then I would suggest you check this object to look that if there are objects using DataRader while are not closed properly.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 22, 2014 2:59 AM
    Moderator
  • Hello Fred,

    Thanks very much for your reply.

    The following is line of code that is throwing this error :-

     db.SaveChanges();

    And demosdfc is object of webservice which contains the function request for saving data to salesforce database.

    following is code inside the request function:-

            [WebMethod]
            public string request(Request request,string flag)
            {
                LoginResult CurrentLoginResult = Login();
                if (CurrentLoginResult == null)
                {
                    return "Login failed";
                }

                SforceService SfdcBinding = new SforceService();
                SfdcBinding.Url = CurrentLoginResult.serverUrl;
                SfdcBinding.SessionHeaderValue = new SDFC.SessionHeader();
                SfdcBinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;

                ZurshRequest__c request__c = new ZurshRequest__c();

                
                request__c.Budget__c = request.Budget;
                request__c.Title__c = request.Title;
                request__c.PreferredModelId__c = request.PreferredModelId.ToString();            
                request__c.Comments__c = request.Comments;
                request__c.Details__c = request.Details;
                request__c.DeliverableDeadline__c = request.DeliverableDeadline.ToString();       
                request__c.Name = request.Title;            
                request__c.RequestorUserId__c = request.RequestorUserId.ToString();
                request__c.RequestStatusId__c = request.RequestStatusId.ToString();

                if (flag == "save")
                {
                    SaveResult[] save = SfdcBinding.create(new sObject[] { request__c });
                    if (save[0].success)
                    {
                        string Id = " ";
                        Id = save[0].id;
                        message = Id;
                    }
                    else
                    {
                        string result = "";
                        result = save[0].errors[0].message;
                        message = result;
                    }
                }
                else if (flag == "update")
                {
                    request__c.Id = request.SalesForceId;
                    UpsertResult[] update = SfdcBinding.upsert("Id", new sObject[] { request__c } );
                }
                else if (flag == "delete")
                {
                    string[] ids = { request.SalesForceId };
                    DeleteResult[] delete = SfdcBinding.delete(ids);
                    message = delete[0].id;
                }
                SfdcBinding.logout();
                return message;
            }

     

    Tuesday, July 22, 2014 6:46 AM
  • Hello,

    From the WebMethod, it seems that this error is not related with it. Since I do not have your project environment, you could try to comment this code and run your code again to see whether the error is caused by this method.

    I notice that you initialize a context object without disposing it, please have a try with Using block as below to ensure the context is disposed:

                using (DFDBEntities db = new DFDBEntities())
    
                {
    
                    DFDBEntities db = new DFDBEntities();
    
                    IQueryable<Order> Orders = db.Orders.AsQueryable();
    
    
                    foreach (var order in Orders)
    
                    {
    
                        order.OrderName = "Changed";
    
                        db.Orders.Attach(order);
    
                        db.Entry(order).State = EntityState.Modified;
    
                        db.SaveChanges();
    
                    }
    
                }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 23, 2014 6:38 AM
    Moderator