views:

2756

answers:

2

Hi all -

I'm trying to bind a gridview to a linq to sql query that's using a stored procedure. When I run the page, i get the following error:

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

var db = new TableItemDataContext();
var q = db.sp_SearchForItems("1","2","3","4"); 
GridView1.DataSource = q;
GridView1.DataBind();

Any Ideas?

+2  A: 

Use the ToList() extension method to convert the query to a list of items.

GridView1.DataSource = q.ToList();

This will also have the effect of running the query at the time of conversion, so you may want to see if just casting to IEnumerable would work.

Edit: to clarify based on the comment trail. The issue turns out to be with the construction of the SPROC and the inability of LINQ to detect the return value. Changing the SPROC as per http://stackoverflow.com/questions/521736/stored-procedure-linq-dmbl-file-unable-to-interpret-the-result-set allowed LINQ to detect the schema after which the SPROC could be changed back.

tvanfosson
Thanks for the quick reply! I used ToList() and I didn't get intellisense (but do have using System.Linq) and when compiled, received "'int' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first arg of type int could be found." Ideas?
asp316
Linq must not be able to determine the schema of the table returned by your stored procedure and thinks that it returns an int. I know that there are some issues with stored procedures that contain temporary tables. Link to question in next comment...
tvanfosson
http://stackoverflow.com/questions/521736/stored-procedure-linq-dmbl-file-unable-to-interpret-the-result-set -- if you can get it to work, it may be able to take the query directly without the ToList().
tvanfosson
Hey! That fixed it. My Stored proc was using a dynamic where clause and the result was to an exec (@sql). I changed it to use normal sql without the dynamic where clause and reimported the sproc into the dbml. Once, complete, I was able to use ToList and change the sproc back. THANKS!!
asp316
A: 

I have nested listviews and i'm using the following LINQ query to bind data.

Dim fy = From f In ds.Getgrpsbyfy _ Select f.FiscalYear, groups = New With _ { _ f.name, f.type, f.Educator, f.contact, f.locationname _ } ListView1.DataSource = fy.ToList() ListView1.DataBind()

Inner listview: ' >

I get the error "datasource is invalid. it must be either ilistsource, ienumerable"

Could you please help me?

Thanks in advance

Harini