Pit Digger
Pit Digger

Reputation: 9780

Bind LINQ Query results to Repeater

I have a query like this

 var businessList = (from u in _DB.Users
join cat in _DB.BusinessCategories on u.UserId equals cat.UserId
select new BusinessDetails(u.Businesses.FirstOrDefault(), u)).Skip(0).Take(10);

dlBusiness.DataSource = businessList.ToList();
dlBusiness.DataBind();

I have Businessname as a member of the Business class . I have a DataList as below

 <asp:DataList ID="dlBusiness" DataKeyField="UserId" runat="server" >
    <ItemTemplate>
            <%#Eval("Business.BusinessName")%>
    </ItemTemplate>
 </asp:DataList>

Here is class defination

public class BusinessDetails
    {

        public BusinessDetails(Business business,User user){
            this._Business = business;
            this._UserDetails = user;

        }

        private string _distance;
        public string Distance
        {
            get { return _distance; }
            set { _distance = value; }
        }

        private Business _Business;
        public Business BusinessData
        {
            get { return _Business; }
            set { _Business = value; }
        }
        private User _UserDetails;

        public User UserDetails
        {
            get { return _UserDetails; }
            set { _UserDetails = value; }
        }


    }

But this displays blank? How can I bind it to the DataList ?

Upvotes: 0

Views: 3020

Answers (2)

Arabela Paslaru
Arabela Paslaru

Reputation: 7074

I think you wanted

<%#Eval("BusinessData.BusinessName")%>

instead of

<%#Eval("Business.BusinessName")%>

in the ItemTemplate tag.

I don't think you have a UserId field on the BusinessDetails class. You should replace that with an unique key column name or remove it. I saw that you can access the BussinessName so the access specifier is public.

The binding is correct. If the dataList control dlBusiness can't find that property an exception is thrown. Maybe the cause of the problem comes from the fact that the businessList returns an empty BussinessName.

Upvotes: 1

kmcc049
kmcc049

Reputation: 2801

I would change it to

DataBinder.Eval(Container.DataItem, "BusinessData.BusinessName")

presuming that the business class has a BusinessName property?

Upvotes: 1

Related Questions