Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to return data from database:


public List<DataModel> GET()
    {
        foreach (var users in db.Users) {
            var user = db.Users.Select(x => new DataModel()
            {
                Email = users.Email,
                UserName = users.UserName,
                FirstName = users.FirstName,
                LastName = users.LastName,
                Age = users.Age,
                Phone = users.Phone,
                Department = users.Department
            });
            return user.ToList();

        }


Here is db class:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole,
int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}




Here is the error:

InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.


Could you please tell what's wrong with this code?

What I have tried:

I have four record in database and the following code return four record but with first record data !!

public List<DataModel> GET()
           {


                   var user = db.Users.Select(x => new DataModel()
                   {
                       Email = db.Users.FirstOrDefault().Email,
                       UserName = db.Users.FirstOrDefault().UserName,
                       FirstName = db.Users.FirstOrDefault().FirstName,
                       LastName = db.Users.FirstOrDefault().LastName,
                       Age = db.Users.FirstOrDefault().Age,
                       Phone = db.Users.FirstOrDefault().Phone,
                       Department = db.Users.FirstOrDefault().Department
                   });
                   return user.ToList();

           }
Posted
Updated 8-Apr-19 7:19am
v2

1 solution

It's probably because this line

foreach (var users in db.Users)


opens a connection and this line

var user = db.Users.Select(


sets up a second connection which is activated when you use .ToList on the following line.

You don't actually need the two loops, this is untested but try something like

public List<DataModel> GET()
    {
            var user = db.Users.Select(x => new DataModel()
            {
                Email = x.Email,
                UserName = x.UserName,
                FirstName = x.FirstName,
                LastName = x.LastName,
                Age = x.Age,
                Phone = x.Phone,
                Department = x.Department
            });

            return user.ToList();

        }
 
Share this answer
 
Comments
Member 12919448 8-Apr-19 13:29pm    
How I missed this :|
You are Awesome
Thank you

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