Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported.

Posted by Chandradev on Geeks with Blogs See other posts from Geeks with Blogs or by Chandradev
Published on Mon, 09 Apr 2012 17:21:20 GMT Indexed on 2012/04/09 17:31 UTC
Read the original article Hit count: 435

Filed under:
Hi

I was doing some test with code first approach in EF. Then while populating the Gridview i was getting error like this


Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().

For solving this error we have to write the code like this

 private void FillGrid()
        {
            using (var Context = new EmpDatabaseContext())
            {
                var query = Context.Emps.Select(m => m);
                //var query = from m in Context.Emps
                //            select m;
               // Gridview1.DataSource = query;
                Gridview1.DataSource = query.ToList();
                Gridview1.DataBind();
            }
        }
  We canot bind Iqueryable directly. We have to change into ToList()

© Geeks with Blogs or respective owner