Tuesday, 27 September 2016

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

In the previous blog, we have seen about Entity Framework.
In this blog, we will see error resolution related to Data binding directly to a store query.

In Visual Studio 2015 I was using Entity Framework and wrote a code to bind data with GridView. I wrote the normal Linq and browse my web page. And surprised to see below line of error: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().
Below is the code which was throwing above error.

using (var ctx = new PracticeEntities())
            {
                var data = from d in ctx.Orders select d;
                gvData.DataSource = data;
                gvData.DataBind();


            }

I changed above code to below and it works for me.


using (var ctx = new PracticeEntities())
            {
                var data = (from d in ctx.Orders select d).ToList();
                gvData.DataSource = data;
                gvData.DataBind();

No comments:

Post a Comment

Dear Readers, Please post your valuable feedback in the comment section if you like this blog or if you have any suggestions. I would love to hear the same from you. Thanks