3

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

        }
    }
Ahmet Güzel
  • 95
  • 1
  • 9

2 Answers2

6

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.

nemesv
  • 138,284
  • 16
  • 416
  • 359
  • I changed but again I got different error on query.Load(); =>A specified Include path is not valid. The EntityType 'WebApplication8.Models.Customer' does not declare a navigation property with the name 'CustomerID'. – Ahmet Güzel Feb 23 '13 at 13:09
  • I just trying to see result my query in gridView by giving CustomerID with "Include" for Explicit Loading Example – Ahmet Güzel Feb 23 '13 at 13:17
  • I deleted "Include" and this time DatabBind gave an error again – Ahmet Güzel Feb 23 '13 at 13:18
  • private void GetSpecificCustomer() { using (var ctx = new northwindContext()) { var query = from mycust in ctx.Customers where mycust.CustomerID == 3 select new { mycust.CustomerID, mycust.FirstName, mycust.LastName }; grdEmployees.DataSource = query; grdEmployees.DataBind(); } } I changed little bit but I'm getting an error in mycust.CustomerID == 3 :/ – Ahmet Güzel Feb 23 '13 at 13:20
  • Message: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(). – Ahmet Güzel Feb 23 '13 at 13:21
  • Operator '==' cannot be applied to operands of type 'string' and 'int' – Ahmet Güzel Feb 23 '13 at 13:32
  • other The using for 'System.Linq' appeared previously in this namespace. – Ahmet Güzel Feb 23 '13 at 13:33
  • I overcame all errors but again grdEmployees.DataBind(); gives and 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(). – Ahmet Güzel Feb 23 '13 at 13:42
  • Ok, I see now what was the problem. The `Load` is not enough in your case you need `ToArray()`. So you need to write `grdEmployees.DataSource = query.ToArray();`. I've also updated my answer. – nemesv Feb 23 '13 at 13:59
0

If u want groups of record.

you should use .Tolist();

such as :

var states = (from s in yourentity.nameTbl
              select s).ToList();

to have better results in get one records, its better use this example because have better performance. such as :

var users = (from s in yourentity.UserTbls
             where s.User == Page.User.Identity.Name
             select s
            ).FirstOrDefault();

For list of record with paging:

int page = 1; // set your page number here
var rooms = (from s in yourentity.yourtable
             where s.User == Page.User.Identity.Name
             orderby s.Id descending
             select new {s.Id,s.Name,s.User}
            ).Skip((page-1)*SetPageSize).Take(SetPageSize).ToList();
IInspectable
  • 46,945
  • 8
  • 85
  • 181
Mohsen
  • 3,569
  • 2
  • 12
  • 4
  • Since this site should be valuable to current and future visitors it would be nice if you could provide an explanation on why this is required. – IInspectable Jul 22 '13 at 22:42
  • This is contextually incorrect.. the underline concerns and concepts here is what is 'IQuerable' and what is 'IEnumerable' .. both are regarded as 'groups of records' (collection behavior) implicitly.. The other underline concept you've referenced here is [Deferred vs Immediate Loading](http://msdn.microsoft.com/en-us/library/bb399393(v=vs.110).aspx): but when you execute `ToList()` you're executing the query immediately (regardless of whether you're setup for deferred or immediate querying from the beginning. – Brett Caswell Oct 10 '14 at 19:21