1

In my project I'm getting an empty array from database. I'm using SQL Server, WebAPI 2 and for the client side AngularJS to display the data from the database. I don't know what is wrong.

Here is the client side code:

$http.get('/api/group')
  .success(function (data, status) {
     $log.info('Data:', data);
     $log.info('Status:', status);
  })
  .error(function (data, status) {
     $log.info('Data:', data);
     $log.info('Status:', status);
  });

WebAPI Controller of group:

public class GroupController : ApiController
{
  public EtfContext db = new EtfContext();

  // GET api/group
  public IQueryable<Group> GetGroups()
  {
    //var groups = from g in db.QR_Groups
    //             select g;
    //return groups;
    return db.QR_Groups;
  }

The Model of group:

public class Group
{
    public Group()
    {
    }

    public int Id { get; set; }
    public string name { get; set; }
    public string code { get; set; }
}

And the DataContext for connecting to the database:

public class EfContext : DbContext
{
  public EfContext() 
    : base("name=EfContext")
    {
    }

  public DbSet<Group> QR_Groups { get; set; }
}

The Table in the database called "QR_Groups" with 3 columns: id, name, code. How do I get the data from the database?

10
  • 1
    Are there actual rows in the database? Commented Jun 29, 2015 at 13:01
  • change it to return db.QR_Groups.ToList() and make sure you have data in your database. Commented Jun 29, 2015 at 13:02
  • @Knelis Yes data is already in the database table. Commented Jun 29, 2015 at 13:04
  • @dotctor I'm using IQueryable. When I try to set ToList then I'm getting an error Commented Jun 29, 2015 at 13:05
  • Also change the return type of GetGroups to IEnumerable<Group> Commented Jun 29, 2015 at 13:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.