c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

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

| | ASP-NET , CSharp

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) 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() 

I got this following error “Data binding to a store query is not support”, when I tried to bind the dropdownlist using the entity framework model object.

Problem:

  public void LoadProductsDropDownList()
        {
               cboProducts.DataTextField = "Name";
               cboProducts.DataValueField = "ProductId";
               cboProducts.DataSource = db.Products;
               cboProducts.DataBind();
                ListItem li = new ListItem("Select Product", "-1");
               cboProducts.Items.Insert(0, li);
        } 

Solution:

I found the cause of this error because of data binding directly is not supported. Instead of that we need to convert to list and then bounded to the dropdownlist. 

  public void LoadProductsDropDownList()
        {
               cboProducts.DataTextField = "Name";
               cboProducts.DataValueField = "ProductId";
               cboProducts.DataSource = db.Products.ToList();
               cboProducts.DataBind();
                ListItem li = new ListItem("Select Product", "-1");
               cboProducts.Items.Insert(0, li);
}