Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DashboardEntities dbentities = new DashboardEntities();
           //var List= dbentities.sp_singlecopltable("2");
           GridView1.DataSource = dbentities.singlecobs("2");
           GridView1.DataBind();


I am binding data to gridview in asp .net Using using Entity Framework
for that I have Code on Page Load as above

But it throwing excepton at
GridView1.DataSource = dbentities.singlecobs("2");

Data source is an invalid type.  It must be either an IListSource, IEnumerable, or IDataSource.


What I have tried:

DashboardEntities dbentities = new DashboardEntities();
//var List= dbentities.sp_singlecopltable("2");
GridView1.DataSource = dbentities.singlecobs("2");
GridView1.DataBind();
Posted
Updated 15-Jan-18 23:43pm

1 solution

I'm going to assume that "singlecobs" returns a single object of some type. A GridView is designed to show a collection of objects of a given type, not a single object. The error is complaining that it can't treat the datasource you are giving it as a collection.

If you want the grid to show a single object\row you need to make a collection that contains your single object. There are various ways to do this, but the below makes an array that contains your object.

GridView1.DataSource = new YourType[] { dbentities.singlecobs("2") };


Replace "YourType" with whatever type is returned by "singlecobs".
 
Share this answer
 

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