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

Posted by Sheonarayan under Error and Solution on 6/25/2014 | Points: 10 | Views : 12022 | Status : [Administrator] | Replies : 0

Problem


I was getting following error when trying to bind the GridView using Entity Framework model collection in EF 6.

using (TrainingDatabaseDB db = new TrainingDatabaseDB())
{
var data = db.PersonalDetails;
GridView1.DataSource = data;
GridView1.DataBind();
}


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

Solution


To fix this bug, we need to tweak the code like this

using (TrainingDatabaseDB db = new TrainingDatabaseDB())
{
var data = db.PersonalDetails.Select(p => p);
GridView1.DataSource = data.ToList();
GridView1.DataBind();
}

Where we explicitly selected each record and converted into the List and then bounded to the GridView.

Hope this helps someone.

Regards,
Sheo Narayan
http://www.dotnetfunda.com



Responses

(No response found.)

Login to post response