c# .net

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

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported., someone asked me to explain?

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.

Instead populate a DbSet with data, for example by calling Load on the DbSet

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

 

Post your comments / questions