Reputation: 310
I am trying to populate a simple ASP repeater from the database. The query works fine and returns several varchar type characters.Here is my code, any insight would help out a lot, im stumped.
Front End
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<p> <%# Eval("FormData")%> </p>
</ItemTemplate>
</asp:Repeater>
Back End
public void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = GetKudosList();
Repeater1.DataBind();
}
public List<string> GetKudosList()
{
using (IntranetEntities KudosContext = new IntranetEntities())
{
var jon = KudosContext.FormInstances.Where(u => u.WorkflowID == 1).Select(u => u.FormData).ToList();
return jon;
}
}
Even if i just create a list object in the back end without touching the DB i still get the error below 'System.String' does not contain a property with the name 'FormData'
Upvotes: 0
Views: 340
Reputation: 310
Thanks for all the Support! I think your suggestions were correct altho they were not working for me. The string "FormData" was not binding to the object
<%# this.GetDataItem().ToString() %>
Upvotes: 0
Reputation: 4526
Your problem is in your linq statement--> you are selecting: .Select(u => u.FormData).ToList();
You will get a List of FormData type, which is probably type of String... (which causes the eval to fail. You are trying to eval property "FormData" on a simple string)
Either use your item as is without eval, or remove the select :
Option 1: You only need the FormData from your object: Use the linq as is, change your repeater template:
<p> <%# Container.DataItem %> </p>
Option 2: You need the whole object and you want to display properties from it, Change your linq:
var jon = KudosContext.FormInstances.Where(u => u.WorkflowID == 1).ToList();
Upvotes: 2
Reputation: 14677
Change your linq query to select an anonymous type instead of string.
var jon = KudosContext.FormInstances
.Where(u => u.WorkflowID == 1)
.Select(u => new { FormData = u.FormData}).ToList();
Now you can use DataBinder
to get the property name from anonymous type:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<p> <%# (string)DataBinder.Eval(e.Item.DataItem, "FormData"); %> </p>
</ItemTemplate>
</asp:Repeater>
Upvotes: 0