locked
Datagrid model binding error- the SelectMethod should return an IQueryable<ItemType> RRS feed

  • Question

  • User-570626059 posted
         I have tried everything. I am using the select method to populate the grid but I am getting this error everytime!!:

    When the DataBoundControl has paging enabled, either the SelectMethod should return an IQueryable<ItemType> or should have all these mandatory parameters : int startRowIndex, int maximumRows, out int totalRowCount



    <asp:GridView runat="server" ID="supplierGrid" ItemType="princeportalweb.models.supplier" SelectMethod="SuppliersGrid_GetData" AutoGenerateColumns="false" AllowPaging="true" PageSize="25" AllowSorting="true"> <Columns> <asp:BoundField DataField="supplierid" HeaderText="id" SortExpression="supplierid" /> <asp:BoundField DataField="suppliername" HeaderText="name" SortExpression="suppliername" /> </Columns> </asp:GridView>
           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;
    
    
    
              
        }
     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)
            {
            }
        }
     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; }
       
    
    
        }
    

    Thursday, July 5, 2018 9:03 PM

All replies

  • User475983607 posted

    Try invoking the query and returning a result set rather than returning a query.

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

    Thursday, July 5, 2018 9:57 PM
  • User1724605321 posted

    Hi skyblue28,

    As a supplement , about this error , you could click here for detail explanation with code sample . You can refer to below link for retrieving and displaying data with model binding and web forms,especially the "Display data from Students and related tables" section:

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data 

    Best Regards,

    Nan Yu

    Friday, July 6, 2018 5:21 AM
  • User-570626059 posted

    Hello trying your suggestion gives error:

    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0266	Cannot implicitly convert type 'System.Collections.Generic.List<PrincePortalWeb.Models.supplier>' to 'System.Linq.IQueryable<PrincePortalWeb.Models.supplier>'. An explicit conversion exists (are you missing a cast?)	PrincePortalWeb	C:\Users\Dan\source\repos\PrincePortalWeb\PrincePortalWeb\Pages\Admin\SupplierEdit.aspx.cs	123	Active
    

    any ideas?

    Friday, July 6, 2018 8:55 AM
  • User-570626059 posted

    Also tried this with the same error ....

            // The return type can be changed to IEnumerable, however to support
            // paging and sorting, the following parameters must be added:
            //     int maximumRows
            //     int startRowIndex
            //     out int totalRowCount
            //     string sortByExpression
            public IQueryable<PrincePortalWeb.Models.supplier> supplierGrid_GetData()
    
            {
    
                SupplierDBModel db = new SupplierDBModel();
    
                var query = (from s in db.suppliers
                             select s).AsQueryable();
    
                return query;           
            }

    I really dont understand what I am doing wrong.

    Friday, July 6, 2018 9:14 AM
  • User475983607 posted

    Sorry, the method return type must match the query results; List<supplier>

    Friday, July 6, 2018 9:43 AM
  • User-570626059 posted

    Hello, 

    I am still getting this error and I cant figure it out.

    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".

    I have tried 

       public List<PrincePortalWeb.Models.supplier> supplierGrid_GetData()
    
            {
    
                SupplierDBModel db = new SupplierDBModel();
    
                var query2 = (from s in db.suppliers
                             select s).ToList();
    
                return query2;           
            }

    as well as the above.

    Whats going wrong?

    Friday, July 6, 2018 9:50 AM
  • User475983607 posted

    Sorry, messed you up a bit.  Go back to returning IQueryable and turn of sorting and paging form now.  Then read the docs.  

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data

    I have not used Web Forms in years and never with Entity Framework or the data wizard but the tutorial above explains how to do databind and sorting.

    Friday, July 6, 2018 9:58 AM
  • User-570626059 posted

    No problem i appreciate the help.... however I have gone back to returned iqueryable and there is no sorting or paging on the grid but I still get the error :(.....

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

    Friday, July 6, 2018 10:09 AM
  • User475983607 posted

    The markup states otherwise.

    <asp:GridView runat="server" ID="supplierGrid"
                ItemType="princeportalweb.models.supplier" SelectMethod="SuppliersGrid_GetData"
                AutoGenerateColumns="false"
                AllowPaging="true" PageSize="25"
                AllowSorting="true">

    Did you go through the tutorial as suggested?

    Friday, July 6, 2018 10:16 AM
  • User-570626059 posted

    Sorry, I missed that. I tried

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

    
    
    
    

    and still same error

    I would like the paging and sorting though. its important for my project.

    Yes i followed the tutorial previously and have been over and over it and I seem to be doing everything as stated. .

    Sorry...

    Friday, July 6, 2018 10:20 AM
  • User-570626059 posted

    tried all suggestions . no joy, thanks for  trying to help

    Friday, July 6, 2018 10:57 AM