Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Error when getting User info from Separate table in foreach loop in ASp.Net Identity

$
0
0

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 in VS 2013, .Net 4.5.1 and Identity 2.0...

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));
    }
<% 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> // Error at this line</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..


Same Error in GridView


public IQueryable<ApplicationUser> BindUsers()
    {
        var users = manager.Users;
        return users;
    }
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" SelectMethod="BindUsers"><AlternatingRowStyle BackColor="White" /><Columns><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></ItemTemplate></asp:TemplateField></Columns><FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /><HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /><PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /><RowStyle BackColor="#FFFBD6" ForeColor="#333333" /><SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /><SortedAscendingCellStyle BackColor="#FDF5AC" /><SortedAscendingHeaderStyle BackColor="#4D0000" /><SortedDescendingCellStyle BackColor="#FCF6C0" /><SortedDescendingHeaderStyle BackColor="#820000" /></asp:GridView>

Help Please..


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>