You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error when getting User info from Separate table in foreach loop #35
I want to Show all Users on a Page. I am storing Profile info in a Separate table called UserProfileInfo. All is working ok. In a foreach loop when I get the FirstName or other fields stored in UserProfileInfo table it give error all the time in WebForms Application...
Here is my code
public class ApplicationUser : IdentityUser
{
public virtual UserProfileInfo UserProfileInfo { get; set; }
}
public class UserProfileInfo
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
WebForm Code Behind to Load all Users
private ApplicationDbContext context;
private UserManager manager;
public List allUser;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
allUser = manager.Users.ToList();
foreach (var usr in manager.Users) {
lblUsers.Text += usr.UserName; // Ok
lblUsers.Text += usr.UserProfileInfo.FirstName; // Error
}
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
context = new ApplicationDbContext();
manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
}
Activity
ranaanees commentedon Feb 24, 2014
Help Please....
ranaanees commentedon Feb 25, 2014
Same Error in GridView
< ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" SelectMethod="BindUsers">
ctolkien commentedon Feb 26, 2014
I think you need to eager load the UserProfileInfo using
ranaanees commentedon Feb 26, 2014
@ctolkien it looks solution, but i am not getting the correct syntax for Include...
private UserManager manager; public List allUser;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{