Click here to Skip to main content
15,921,212 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am new to entity framework and found that the CodeFirst release is okay. So I decided to create a simple asp.net webforms application to test this. Populated a table with some data and tried to retrieve the data with the following code:

VB
Imports System.Data.Entity

Public Class AdmonHRMDbContext
    Inherits DbContext

    Public Property Application_User As DbSet(Of User)
End Class



VB
Public Class User
    Public Property UserId As Int32
    Public Property LoginName As String
    Public Property Password As String
End Class


Public Class UserRepository

    Private context As AdmonHRMDbContext

    Public Sub New()
        context = New AdmonHRMDbContext
    End Sub

    Public Function GetUsers() As IQueryable(Of User)
        Return context.Application_User
    End Function
End Class




VB
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim u As New BusinessLogic.UserRepository()

    GridView1.DataSource = u.GetUsers
    GridView1.DataBind()
End Sub



below is the exception I got:
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().

I don't know what to do please help

Aweklin
Posted

you can't bind to IQueryable as it only desribes how and what data to retrieve. You'll have to actually query the data first, e.g. by using the ToList extension method.

C#
GridView1.DataSource = u.GetUsers.ToList()
 
Share this answer
 
Comments
RaisKazi 23-Sep-11 9:17am    
Agree. 5!
Yuri Vital 23-Sep-11 9:24am    
Me too. 5 !
Simon Bang Terkildsen 23-Sep-11 9:35am    
Thanks to both of you.
Simon Bang Terkildsen 23-Sep-11 9:31am    
OP Wrote
Thanks Simon,

GridView1.DataSource = u.GetUsers.ToList

I have tried that but got this error:


EntityCommandExecutionException was unhandled by user code:
An error occurred while executing the command definition. See the inner exception for details.
Simon Bang Terkildsen 23-Sep-11 9:33am    
Hmm I don't know why it would throw an exception, can you post the inner exception, please.
Try following code :

VB
Public Function GetUsers() As List(Of User)
     Return context.Application_User.ToList()
End Function



-Sagar Solanki
 
Share this answer
 
Comments
Simon Bang Terkildsen 23-Sep-11 9:35am    
I already suggested that he should use the ToList extension method, but for some reason it throws an exception, see the comments to solution 1.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900