Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when getting User info from Separate table in foreach loop #35

Open
ranaanees opened this issue Feb 24, 2014 · 4 comments
Open

Error when getting User info from Separate table in foreach loop #35

ranaanees opened this issue Feb 24, 2014 · 4 comments
Labels

Comments

@ranaanees
Copy link

Hello,

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));
    }
HTML For Loop with same error
     <% if (allUser.Count() > 0 )
            { %>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>User Name</th>
                    <th>Name</th>
                </tr>
            </thead>
            <tbody>
                <% foreach (var user in allUser)
                   { %>
                <tr>
                    <td><%: user.Id %></td>
                    <td><%: user.UserName %></td>
                    <td><%: user.UserProfileInfo.FirstName %></td>
                </tr>
                <% } %>
            </tbody>
        </table>
        <% } else { %>
        <p class="text-danger">No User Found.</p>
        <% } %>

Error
An exception of type 'System.NullReferenceException' occurred in App_Web_4cfuyuel.dll but was not handled in user code

Need help...

@ranaanees
Copy link
Author

Help Please....

@ranaanees
Copy link
Author

Same Error in GridView

    public IQueryable<ApplicationUser> BindUsers()
    {
        var users = manager.Users;
        return users;
    }

< ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" SelectMethod="BindUsers">

                <asp:TemplateField HeaderText="Id">
                    <ItemTemplate>
                        <asp:Label ID="lblId" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="User Name">
                    <ItemTemplate>
                        <asp:Label ID="lblUserName" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("UserProfileInfo.FirstName") %>'></asp:Label> //Error at this line
                    </ItemTemplate>
                </asp:TemplateField>

@ctolkien
Copy link

I think you need to eager load the UserProfileInfo using

.Include(x => x.UserProfileInfo)

@ranaanees
Copy link
Author

@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)
{

            allUser = from m in manager.Users.Include(p => p.UserProfileInfo)
                           select m;

            --Error: how to write linq query with include in this scenario....?

       }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants