none
Entity framework model binding to data grid select method error- should return an IQueryable<ItemType> RRS feed

  • Question

  • Hello, I am pulling my hair out with this error. I dont know what is causing it and I have tried everything I can think of.

    I am trying to populate the grid using the select method.

    Grid:

         <asp:GridView ID="GridView1" ItemType="PrincePortalWeb.Models.supplier" SelectMethod="GridView1_GetData" runat="server"></asp:GridView>

    My select method:

        public IQueryable<supplier> SuppliersGrid_GetData([Control("searchText")] string search)
    
            {
    
                SupplierDBModel db = new SupplierDBModel();            
    
                var query = from s in db.suppliers
                            where s.suppliername.ToUpper() == "SUPPLIER"
                            select s;                                 
                           
                return query;
              
        }


    My supplier context:

    public partial class SupplierDBModel : DbContext
        {    
    
    
    
    
            public SupplierDBModel()
                : base("name=SupplierDBModel")
            {
    
    
               // Database.SetInitializer(new MigrateDatabaseToLatestVersion<SupplierDBModel, EF6Console.Migrations.Configuration>());
    
    
            }
    
            public virtual DbSet<supplier> suppliers { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
            }
        }


    supplier model:

    public

    partial class supplier { public int supplierid { get; set; } [Required] [StringLength(50)] public string suppliername { get; set; } [StringLength(50)] public string address1 { get; set; } [StringLength(50)] public string county { get; set; } [StringLength(50)] public string country { get; set; } [StringLength(50)] public string telephone { get; set; } [StringLength(50)] public string address2 { get; set; } [StringLength(11)] public string postcode { get; set; } [StringLength(20)] public string webstatus { get; set; } [StringLength(50)] public string status { get; set; } [StringLength(50)] public string LastreviewedBy { get; set; } [Column(TypeName = "datetime2")] public DateTime Whencompliant { get; set; } [Column(TypeName = "datetime2")] public DateTime Reviewdate { get; set; } }

    error :

    The Select Method must return one of "IQueryable<PrincePortalWeb.Models.Supplier>" or "IEnumerable<PrincePortalWeb.Models.Supplier>" or "PrincePortalWeb.Models.Supplier" when ItemType is set to "PrincePortalWeb.Models.Supplier".

    What am i doing wrong? 

    thanks



    • Edited by danhickson Friday, July 6, 2018 10:36 AM
    • Moved by CoolDadTx Friday, July 6, 2018 1:56 PM EF related
    Friday, July 6, 2018 10:17 AM

All replies

  • Try
    return query.AsQueryable();
    Friday, July 6, 2018 10:27 AM
  • hello thanks for reply. yes tried that one already . same error 

    The Select Method must return one of "IQueryable<PrincePortalWeb.Models.Supplier>" or "IEnumerable<PrincePortalWeb.Models.Supplier>" or "PrincePortalWeb.Models.Supplier" when ItemType is set to "PrincePortalWeb.Models.Supplier".

      public IQueryable<PrincePortalWeb.Models.supplier> GridView1_GetData()
            {
                SupplierDBModel dBModel = new SupplierDBModel();
    
                var query = from s in dBModel.suppliers select s;
                return query.AsQueryable();
            }

    • Edited by danhickson Friday, July 6, 2018 10:35 AM
    Friday, July 6, 2018 10:34 AM
  • Hi you need to implement the IEnumerable and/or  the IQueryable interfaces by your class "Supplier":

    public partial class supplier: IEnumerable ,  IQueryable {...}

    Then, make sure you implement the required members of  those interfaces.

    You can find many articles online about how to implement those interfaces.



    Friday, July 6, 2018 11:04 AM
  • EF issues can be discussed at the EF forum.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    public List<supplier> SuppliersGrid_GetData([Control("searchText")] string search)
    {
                var suppliers = new List<supplier>();
    
                using (var db = new SupplierDBModel() 
                {          
    
                suppliers = (from s in db.suppliers
                            where s.suppliername.ToUpper() == "SUPPLIER"
                            select s).ToList(); 
                 }
                           
                return suppliers;
        }

    Friday, July 6, 2018 11:15 AM
  • EF issues can be discussed at the EF forum.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    public List<supplier> SuppliersGrid_GetData([Control("searchText")] string search)
    {
                var suppliers = new List<supplier>();
    
                using (var db = new SupplierDBModel() 
                {          
    
                suppliers = (from s in db.suppliers
                            where s.suppliername.ToUpper() == "SUPPLIER"
                            select s).ToList(); 
                 }
                           
                return suppliers;
        }

    OK thanks, this give the same error.
    Friday, July 6, 2018 11:24 AM
  • Any example of how to do this? in the tutorials Ive been following for model binding to grid, they didnt need to do this.
    Friday, July 6, 2018 11:24 AM
  • Hello, I am pulling my hair out with this error. I dont know what is causing it and I have tried everything I can think of.

    I am trying to populate the grid using the select method.

    Grid:

         <asp:GridView ID="GridView1" ItemType="PrincePortalWeb.Models.supplier" SelectMethod="GridView1_GetData" runat="server"></asp:GridView>

    My select method:

        public IQueryable<supplier> SuppliersGrid_GetData([Control("searchText")] string search)
    
            {
    
                SupplierDBModel db = new SupplierDBModel();            
    
                var query = from s in db.suppliers
                            where s.suppliername.ToUpper() == "SUPPLIER"
                            select s;                                 
                           
                return query;
              
        }

    My supplier context:

    public partial class SupplierDBModel : DbContext
        {    
    
    
    
    
            public SupplierDBModel()
                : base("name=SupplierDBModel")
            {
    
    
               // Database.SetInitializer(new MigrateDatabaseToLatestVersion<SupplierDBModel, EF6Console.Migrations.Configuration>());
    
    
            }
    
            public virtual DbSet<supplier> suppliers { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
            }
        }

    supplier model:

    public

    partial class supplier { public int supplierid { get; set; } [Required] [StringLength(50)] public string suppliername { get; set; } [StringLength(50)] public string address1 { get; set; } [StringLength(50)] public string county { get; set; } [StringLength(50)] public string country { get; set; } [StringLength(50)] public string telephone { get; set; } [StringLength(50)] public string address2 { get; set; } [StringLength(11)] public string postcode { get; set; } [StringLength(20)] public string webstatus { get; set; } [StringLength(50)] public string status { get; set; } [StringLength(50)] public string LastreviewedBy { get; set; } [Column(TypeName = "datetime2")] public DateTime Whencompliant { get; set; } [Column(TypeName = "datetime2")] public DateTime Reviewdate { get; set; } }

    also tried 

    return query.AsQueryable();

    error :

    The Select Method must return one of "IQueryable<PrincePortalWeb.Models.Supplier>" or "IEnumerable<PrincePortalWeb.Models.Supplier>" or "PrincePortalWeb.Models.Supplier" when ItemType is set to "PrincePortalWeb.Models.Supplier".

    What am i doing wrong?  

    thanks


    Friday, July 6, 2018 11:27 AM
  • EF issues can be discussed at the EF forum.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    public List<supplier> SuppliersGrid_GetData([Control("searchText")] string search)
    {
                var suppliers = new List<supplier>();
    
                using (var db = new SupplierDBModel() 
                {          
    
                suppliers = (from s in db.suppliers
                            where s.suppliername.ToUpper() == "SUPPLIER"
                            select s).ToList(); 
                 }
                           
                return suppliers;
        }

    OK thanks, this give the same error.

    The only way it didn't work is that you didn't duplicate the code shown to you.

    And this is just another reason I stay away from using EF code first (cheesecake), and will always stick with EF database first.

    I also never bind an EF persistence model object directly to any control ever.

    https://juristr.com/blog/2012/10/lessions-learned-dont-expose-ef-entities-to-the-client-directly/

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    Friday, July 6, 2018 11:44 AM
  • EF issues can be discussed at the EF forum.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    public List<supplier> SuppliersGrid_GetData([Control("searchText")] string search)
    {
                var suppliers = new List<supplier>();
    
                using (var db = new SupplierDBModel() 
                {          
    
                suppliers = (from s in db.suppliers
                            where s.suppliername.ToUpper() == "SUPPLIER"
                            select s).ToList(); 
                 }
                           
                return suppliers;
        }

    OK thanks, this give the same error.

    The only way it didn't work is that you didn't duplicate the code shown to you.

    And this is just another reason I stay away from using EF code first (cheesecake), and will always stick with EF database first.

    I also never bind an EF persistence model object directly to any control ever.

    https://juristr.com/blog/2012/10/lessions-learned-dont-expose-ef-entities-to-the-client-directly/

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    I duplicated the code exactly and still get the same error.

    What would you suggest using then? I am new to software development, I just want the fastest way to get a grid that can be edited,deleted, sorted ,filtered. 

    Thanks

    Friday, July 6, 2018 11:50 AM
  • What are you using MVC or what?

    Friday, July 6, 2018 11:53 AM
  • webforms

    thanks

    Friday, July 6, 2018 11:55 AM
  • webforms

    thanks

    grid.datasource = (from s in db.suppliers
                           
    where s.suppliername.ToUpper() == "SUPPLIER"
                           
    select s).ToList();                                 
                          

    I would try that and forget about whatever else you are trying to follow.

    Friday, July 6, 2018 12:16 PM
  • will i still be able to sort on the grid or will i have to write methods for this?
    Friday, July 6, 2018 1:09 PM
  • will i still be able to sort on the grid or will i have to write methods for this?

    the grid has its own sort functions that I know about that would be based on the bound datasource.
    Friday, July 6, 2018 3:25 PM
  • try the below code

     public IQueryable<supplier> SuppliersGrid_GetData([Control("searchText")] string search)
    
            {
    
                SupplierDBModel db = new SupplierDBModel();            
    
                IQueryable<supplier> query = from s in db.suppliers
                            where s.suppliername.ToUpper() == "SUPPLIER"
                            select s;                                 
                           
                return query;
              
        }

    Sunday, July 8, 2018 11:22 AM