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();


Comments

Popular

What's new in C# 6.0 language - All new features of C# 6.0 for developers

SharePoint Interview Questions and Answers