Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumerable'
it points to this line:
@foreach (var row in data){
What's the problem?
oh some more info:
The page this code is on also opens up a database and requests pages and page tile tags etc and prints it out on that page (it's like a render page) so the url is like this pagename?id=ID however I also want a foreach statement to print out certain pages.
Database.QuerySingle() returns one DynamicRecord. Database.Query() returns a collection of them. You use foreach on collections, not a single object. Change your method call to Database.Query if you expect more than one record in the results.
CS1061: 'System.Collections.Generic.IEnumerable<dynamic>' does not contain a definition for 'pName' and no extension method 'pName' accepting a first argument of type 'System.Collections.Generic.IEnumerable<dynamic>' could be found (are you missing a using directive or an assembly reference?)
var ID = Request["ID"];
var SQLSELECT = "SELECT * FROM Pages WHERE ID=@0 and pCategory = 'C#' ";
var db = Database.Open("TCGMobile");
var data = db.QuerySingle(SQLSELECT, ID);
var pName = data.pName;
var pTitle = data.pTitle;
var mKeywords = data.mKeywords;
var pCategory = data.pCategory;
var mDescription = data.mDescription;
var pBody = data.pBody;
var pSiteMap = data.pSiteMap;
Page.mTitle = pName;
Page.mKeywords = mKeywords;
Page.mDescription = mDescription;
}
<table>
<tr>
I WANT FOR EACH STATEMENT HERE!!
<td>@pName</td>
</tr>
</table>
<h4>@pTitle</h4>
@Html.Raw(pBody)
OK. When you use QuerySingle, you get one dynamic object. You can reference its properties eg:
var product = db.QuerySingle("SELECT * From products where ProductId = 1");
@product.ProductName
When you use Query, you get a collection. The collection doesn't have a ProductName property, but each item within the collection does, so you need to access the items. You can do that using foreach:
var products = db.Query("SELECT * From products");
@foreach(var product in products){
@product.ProductName <br />
}
You can also do it by referencing items according to their position in the collection:
var product = products.ElementAt(0);
@product.ProductName
CriticalErro...
Member
448 Points
408 Posts
Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumerable'
Jul 14, 2012 03:35 PM|LINK
I always keep getting this error:
Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumerable'
it points to this line:
@foreach (var row in data){What's the problem?
oh some more info:
The page this code is on also opens up a database and requests pages and page tile tags etc and prints it out on that page (it's like a render page) so the url is like this pagename?id=ID however I also want a foreach statement to print out certain pages.
My Site | My Blog
Mikesdotnett...
All-Star
155593 Points
19979 Posts
Moderator
MVP
Re: Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumera...
Jul 14, 2012 03:40 PM|LINK
Database.QuerySingle() returns one DynamicRecord. Database.Query() returns a collection of them. You use foreach on collections, not a single object. Change your method call to Database.Query if you expect more than one record in the results.
Web Pages CMS | My Site | Twitter
CriticalErro...
Member
448 Points
408 Posts
Re: Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumera...
Jul 14, 2012 03:42 PM|LINK
if I do that then I get this error:My Site | My Blog
GmGregori
Contributor
5564 Points
749 Posts
Re: Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumera...
Jul 14, 2012 04:26 PM|LINK
Are you sure that exists a 'pName' field in your table?
CriticalErro...
Member
448 Points
408 Posts
Re: Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumera...
Jul 14, 2012 04:43 PM|LINK
Yes here is the code:
var ID = Request["ID"]; var SQLSELECT = "SELECT * FROM Pages WHERE ID=@0 and pCategory = 'C#' "; var db = Database.Open("TCGMobile"); var data = db.QuerySingle(SQLSELECT, ID); var pName = data.pName; var pTitle = data.pTitle; var mKeywords = data.mKeywords; var pCategory = data.pCategory; var mDescription = data.mDescription; var pBody = data.pBody; var pSiteMap = data.pSiteMap; Page.mTitle = pName; Page.mKeywords = mKeywords; Page.mDescription = mDescription; } <table> <tr> I WANT FOR EACH STATEMENT HERE!! <td>@pName</td> </tr> </table> <h4>@pTitle</h4> @Html.Raw(pBody)My Site | My Blog
Mikesdotnett...
All-Star
155593 Points
19979 Posts
Moderator
MVP
Re: Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumera...
Jul 14, 2012 07:21 PM|LINK
What do you want your foreach statement to do? There is only one database record in "data" because you have used QuerySingle.
Web Pages CMS | My Site | Twitter
CriticalErro...
Member
448 Points
408 Posts
Re: Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumera...
Jul 14, 2012 08:29 PM|LINK
I wanted it to print out all pages in the category C#. It saves me manually linking them :)
My Site | My Blog
Mikesdotnett...
All-Star
155593 Points
19979 Posts
Moderator
MVP
Re: Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumera...
Jul 14, 2012 08:37 PM|LINK
As I said before, if you want mutilpe rows in your results, you need to use Database.Query, not QuerySingle. Your code uses QuerySingle. Change it.
Web Pages CMS | My Site | Twitter
CriticalErro...
Member
448 Points
408 Posts
Re: Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumera...
Jul 14, 2012 08:39 PM|LINK
Yeah I know Mike, but like I said in third post I get that error:
http://forums.asp.net/post/5066109.aspx
My Site | My Blog
Mikesdotnett...
All-Star
155593 Points
19979 Posts
Moderator
MVP
Re: Cannot implicitly convert type 'WebMatrix.Data.DynamicRecord' to 'System.Collections.IEnumera...
Jul 14, 2012 09:22 PM|LINK
OK. When you use QuerySingle, you get one dynamic object. You can reference its properties eg:
var product = db.QuerySingle("SELECT * From products where ProductId = 1");
@product.ProductName
When you use Query, you get a collection. The collection doesn't have a ProductName property, but each item within the collection does, so you need to access the items. You can do that using foreach:
var products = db.Query("SELECT * From products"); @foreach(var product in products){ @product.ProductName <br /> }You can also do it by referencing items according to their position in the collection:
Web Pages CMS | My Site | Twitter