tuckerjt07
tuckerjt07

Reputation: 922

Error When Databinding to Control

I am having trouble getting my datasource linked to my repeater through this code

protected void Page_Load(object sender, EventArgs e)
{
    //HiddenField used as a placholder
    HiddenField username = list.FindControl("username") as HiddenField;
    //list is a DataList containing all of the user names
    list.DataSource = Membership.GetAllUsers();
    list.DataBind();
    //Creates a string for each user name that is bound to the datalist
    String user = username.Value;
    //profilelist is a repeater containing all of the profile information
    //Gets the profile of every member that is bound to the DataList
    //Repeater is used to display tables of profile information for every user on
    // the site in a single webform
    profilelist.DataSource = Profile.GetProfile(user);
    profilelist.DataBind();

}

I am getting the error message

An invalid data source is being used for profilelist. A valid data source must implement either IListSource or IEnumerable.

Upvotes: 0

Views: 1108

Answers (3)

tuckerjt07
tuckerjt07

Reputation: 922

I forgot to post this up but for anyone that needs to do something similar here is the code behind that works

protected void Page_Load(object sender, EventArgs e)
{
    List<MembershipUserCollection> usernamelist = new List<MembershipUserCollection>();
    usernamelist.Add(Membership.GetAllUsers());
    List<ProfileCommon> myProfileList = new List<ProfileCommon>();
        foreach (MembershipUser user in usernamelist[0])
        {
            string username = user.ToString();
            myProfileList.Add(Profile.GetProfile(username));
            Label emailLabel = profilelist.FindControl("EmailLabel") as Label;
        }
}

At the moment this is displaying about 15 user names and providing an ability to link to each of theses users respective profiles.

Upvotes: 0

Etch
Etch

Reputation: 3054

Well the reason why it will not work is because Profile.GetProfile returns ProfileCommon. As the error states the type you set profilelist.Datasource equal to, must be IListSource or IEnumerable.

I would suggest not using a repeater since you don't have actual repeating data to display.

EDIT

I think this is what you want to do.

        IEnumerable<ProfileCommon> myProfileList = new IEnumerable<ProfileCommon>();

        foreach(var user in userlist)
        {
             myProfileList.Add(Profile.GetProfile(user));
        }

        profilelist.datasource = myProfileList;

Upvotes: 2

ChrisLively
ChrisLively

Reputation: 88044

Your going about this wrong. As Etch said, a repeater is for lists of things. GetProfile doesn't return a list.

You're better off just putting your controls in a panel and assigning them in the "list" controls ondatabinding event.

In other words, you don't need a repeater here.

Upvotes: 1

Related Questions