Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am sending jquery ajax call to get this data , so during this call , sometimes i recevive this error

XML
public static List<object> GetExcelDataforOjective(GoogleChartReportFilters objFilter, int? companyId, string ObjectiveStatus)
        {
            ActusSystemDataContext _db = new ActusSystemDataContext();
            int dataForYear = Company.GetCompaniesCurrentBusinessyear().company_StartDate.Year;
            System.Data.Linq.ISingleResult<usp_GetEmployeeListForObjectiveChartResult> objectiveResult = null;
            objectiveResult = _db.usp_GetEmployeeListForObjectiveChart(dataForYear,
                                                                    companyId, objFilter.DateFilter, objFilter.MoreThanLessThanStatus,
                                                                    objFilter.MoreThanLessThanValue, objFilter.ObjectivesStatus, objFilter.ObjectivePeriod);
            List<object> lstObj = new List<object>();

            foreach (var result in objectiveResult.ToList())
            {
                if (result.Objective_Status == ObjectiveStatus)
                {
                    ActusLibrary.Objectives obj = new ActusLibrary.Objectives();

                    obj.ObjectiveStatus = result.Objective_Status;
                    obj.EmpName = result.EmployeeName;
                    obj.Business_unit = result.Business_unit;
                    obj.Department = result.Department;
                    obj.Team = result.Team;
                    obj.Location = result.Location;
                    obj.ManagerName = result.ManagerName;
                    lstObj.Add(obj);
                }
            }
            return lstObj;
        }
Posted
Updated 28-May-15 20:02pm
v2
Comments
Sergey Alexandrovich Kryukov 29-May-15 2:04am    
Error? What error?
—SA
Torakami 29-May-15 2:23am    
query result can not be enumerated more than once during foreach loopinng

1 solution

I assume you have a concurrency problem there because objectiveResult apparently is a class member and not a local variable. So while one call to this method is about to execute ToList() on objectiveResult, another call overwrites it as it finishes the the query call to _db.usp_GetEmployeeListForObjectiveChart(..) and then both try to execute ToList() on the same query result object. Make objectiveResult a local variable of that method and it should be good.

edit: Illustration of variable declaration scope:
C#
class SomeClass
{
    int SomeMemberVariable; // declared as class member

    void SomeMethod()
    {
        int someLocalVariable; // declared locally in method

        SomeMemberVariable = 1; // class member (analog to objectiveResult)
    }
}

You declared objectiveResult as a class member like SomeMemberVariable in this example. You need to declare it locally like someLocalVariable instead.
 
Share this answer
 
v2
Comments
_Asif_ 29-May-15 2:41am    
+5
Torakami 29-May-15 3:21am    
objectiveResult is my local variable only
Sascha Lefèvre 29-May-15 3:29am    
I assume you misunderstand: You use it locally but you didn't declare it locally. The declaration is the line where the type name is specified in front of it.
Torakami 29-May-15 3:35am    
I really didnt get this point , can you please show me what do u mean by that by showing an example for this for declaration ,

is this mean by declaring with new keyword and then assigning the value to it. ?

sorry for the inconvenience but may be you the one who is genius who can explain me very well .. thanks brother
Sascha Lefèvre 29-May-15 3:47am    
Please see my answer above, after "edit".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900