Hi,
I am seeking to use a TypedList, and iterate through the results. When I run this code I'll post below, the compiler throws this error:
Unable to cast object of type 'DAL.TypedListClasses.ManifestationsBoardRow' to type 'DAL.TypedListClasses.ManifestationBoardRow'.
This error is thrown here:
foreach (ManifestationBoardRow dr in manifestations)
The entire block of code is here:
protected void Page_Load(object sender, EventArgs e)
{
ISortExpression sorter = new SortExpression(ManifestationsFields.CreatedDateTime | SortOperator.Descending);
int maxRows = 10;
bool allowDuplicates = false;
ManifestationsBoardTypedList manifestations =
ManifestationController.GetManifestationsBoard(maxRows, ManifestationsFields.CreatedDateTime, SortOperator.Descending, allowDuplicates);
StringBuilder html = new StringBuilder();
if (manifestations != null)
{
html.Append("Count: " + manifestations.Count + "<BR>");
html.Append("<table border='0'>");
foreach (ManifestationBoardRow dr in manifestations)
{
html.Append("<tr><td>Username:</td><td>" + dr.UserName + "</td></tr>");
html.Append("<tr><td>Title:</td><td>" + dr.Title + "</td></tr>");
html.Append("<tr><td>Manifested:</td><td>" + dr.Manifestation + "</td></tr>");
}
}
else
{
html.Append("<TR><TD colspan='2'>There are no manifestations to display. Quick, go manifest something, and tell us about it! :-)</TD></TR>");
}
html.Append("</table>");
manifestationsGgridDisplayLabel.Text = html.ToString();
}
and:
public static ManifestationsBoardTypedList GetManifestationsBoard(int maxRows, EntityField sortEntityField, SortOperator sortOperator, bool allowDuplicates)
{
ManifestationsBoardTypedList manifestationsBoard = new ManifestationsBoardTypedList();
ISortExpression sorter = new SortExpression(sortEntityField | sortOperator);
manifestationsBoard.Fill(maxRows, sorter, allowDuplicates);
if (manifestationsBoard.Count > 0)
{
return manifestationsBoard;
}
else
{
return null;
}
}
Can you please help me understand what is happening here, and how I can have this work?
Also, I was able to have it work this way:
for (index = 0; index < manifestations.Count; index++)
{
html.Append("<tr><td>Username:</td><td>" + manifestations[index].UserName + "</td></tr>");
html.Append("<tr><td>Title:</td><td>" + manifestations[index].Title + "</td></tr>");
html.Append("<tr><td>Manifested:</td><td>" + manifestations[index].Manifestation + "</td></tr>");
}
And would love to also have a deeper understanding of why the foreach isn't working.
Thank you very much,
Mike