Click here to Skip to main content
15,888,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a requirement to print all employee Service Card when user click on one button,i have entered all employee records in one table. problem is that when i click on the button only one employee record is shown. i am using following code to test my requirements .
C#
protected void EmpSC_Click(object sender, EventArgs e)
      {
          EmployeeSpDataSet dataset = new EmployeeSpDataSet();

          var AllEmp = _service.GetAllEmployeeDuty().OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).Take(3).ToList();
          if (AllEmp != null)
          {
              for (int i = 1; i <= 3; i++)
              {
                  var empsp = AllEmp.FirstOrDefault();
                      var emp = _service.GetAllEmployee().Where(x => x.Id == empsp.Employee_Id).FirstOrDefault();
                      dataset.EmployeeScpass.Rows.Add(emp.Id,
                          emp.Name,
                          emp.Cnic,
                          empsp.ToSectorName,
                          emp.ImgBody
                          );
                  AllEmp.Remove(empsp);
                  }



          }

is there is any way to get that thing on track.
Posted
Comments
Sinisa Hajnal 27-Apr-15 2:55am    
Can you confirm that AllEmp contains three elements BEFORE the loop? that is, does your LINQ selects correct elements?

Also, why not use foreach instead of for loop ? You wouldn't have to remove anything that way.
Sajid227 27-Apr-15 3:00am    
i got just one record from all employee in the report,i use remove so that when employee is used up then next employee its record goes to dataset
Sinisa Hajnal 27-Apr-15 6:00am    
And you then assign this dataset with three records to the report and it shows only one? OR your dataset contains only single employee?

1 solution

You seem to be getting the first row again and again.
C#
var empsp = AllEmp.FirstOrDefault();
var emp = _service.GetAllEmployee().Where(x => x.Id == empsp.Employee_Id).FirstOrDefault();
 
Share this answer
 
Comments
Sinisa Hajnal 27-Apr-15 2:54am    
He is removing current iteration element at the end of the loop and then gets the first of the remaining elements.
Sajid227 27-Apr-15 3:01am    
u right it but problem is that i got just one record not three record separatly
Sajid227 27-Apr-15 3:05am    
loop is working properly it fetch employee ,problem is that it is not showing all employee record in the report separatly

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