I'm trying this part of codes on "northwind" database.However I'm getting DataBind error
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetSpecificCustomer();
}
}
private void GetSpecificCustomer()
{
using (var ctx = new northwindContext())
{
var query = ctx.Customers.Include("CustomerID").Take(3);
grdEmployees.DataSource = query;
grdEmployees.DataBind(); =>NotSupportedException was unhandled by user code(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().)
}
}
The exception contains what you need to do:
...Instead populate a DbSet with data ...
So you need to evaluate the query. The exception mention the Load method but because you anyway need to store the result locally the easiest solution is to to call ToArray() on your query when assign it to grdEmployees.DataSource.
var query = ctx.Customers.Include("CustomerID").Take(3);
grdEmployees.DataSource = query.ToArray();
grdEmployees.DataBind();
The ToArray method will execute the query the returns the result set in an array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With