【问题标题】:How to populate a gridview from Active Directory in Visual C#如何在 Visual C# 中从 Active Directory 填充网格视图
【发布时间】:2016-11-15 03:07:17
【问题描述】:

我目前正在使用带有 ASP.NET 框架的 Visual C#,并且我正在尝试通过 DataTable 填充 GridView。正在从 Active Directory 获取信息。

我的 gridview 是这样声明的:

<asp:GridView ID="grdvList" runat="server" AutoGenerateColumns="False" Width="567px">
    <Columns>
        <asp:BoundField HeaderText="Name" ReadOnly="True" />
        <asp:BoundField HeaderText="Phone" ReadOnly="True" />
        <asp:BoundField HeaderText="Email" ReadOnly="True" />
    </Columns>
</asp:GridView>

我尝试填充 gridview 的代码如下:

DirectoryEntry entry = new DirectoryEntry(ConfigurationManager.AppSettings["LDAP"]);

DirectorySearcher search = new DirectorySearcher(entry)
        {
            SearchScope = SearchScope.Subtree,
            Filter = "(&(objectClass=user)(physicalDeliveryOfficeName=Dartmouth))"
        };

search.PropertiesToLoad.Add("sAMAccountName");

SearchResultCollection result = search.FindAll();

DataTable table = new DataTable();
DataRow dr = null;

//Add columns to DataTable
table.Columns.Add("Name", System.Type.GetType("System.String"));
table.Columns.Add("Phone", System.Type.GetType("System.String"));
table.Columns.Add("Email", System.Type.GetType("System.String"));

foreach (SearchResult sr in uList)
{
    dr = table.NewRow();

    DirectoryEntry DE = sr.GetDirectoryEntry();
    dr["Name"] = DE.Properties["givenName"].Value.ToString();
    dr["Phone"] = DE.Properties["telephoneNumber"].Value.ToString();
    dr["Email"] = DE.Properties["mail"].Value.ToString();

    table.Rows.Add(dr);
}

table.AcceptChanges();

grdvList.DataSource = table;
grdvList.DataBind();

目前当我运行它时,它会抛出一个

对象引用未设置为对象错误的实例

在这一行:

dr["Phone"] = DE.Properties["telephoneNumber"].Value.ToString();

非常感谢任何帮助!

【问题讨论】:

标签: c# asp.net gridview datatable ldap


【解决方案1】:

在您的场景中,您可以避免使用旧技术的 DataTableDataSet。这些天来,我们尽量不使用它们,除非我们别无选择。

对于NullReferenceException,您需要确保DE.Properties["mail"] 在获取Value 之前不为空。

例如,

DirectoryEntry entry = new DirectoryEntry(ConfigurationManager.AppSettings["LDAP"]);
DirectorySearcher search = new DirectorySearcher(entry)
{
    SearchScope = SearchScope.Subtree,
    Filter = "(&(objectClass=user)(physicalDeliveryOfficeName=Dartmouth))"
};
search.PropertiesToLoad.Add("sAMAccountName");
SearchResultCollection result = search.FindAll();

var users = result.Cast<SearchResult>().Select(sr => sr.GetDirectoryEntry())
    .Select(de => new 
    {
        Name = de.Properties["Name"] != null ? de.Properties["Name"].Value.ToString() : "",
        Phone = de.Properties["Phone"] != null ? de.Properties["Phone"].Value.ToString() : "",
        Email = de.Properties["Email"] != null ? de.Properties["Email"].Value.ToString() : "",
    }).ToList();

grdvList.DataSource = users;
grdvList.DataBind();

【讨论】:

  • 好的,这是朝着正确方向迈出的一步。您的代码中唯一的错误是我们需要Name = de.Properties["Name"].Value != null 而不是Name = de.Properties["Name"] != null。我的 gridview 填充了适当数量的行,但是其中没有任何内容...i.imgur.com/eVaodTy.png
  • 绑定字段仍需要 datafield 属性。例如,&lt;asp:boundfield datafield="Name" HeaderText="Name" ReadOnly="True""/&gt;
  • 成功了!唯一剩下的问题是在您发布的原始代码中,您没有使用正确的 LDAP 名称。例如 de.properties["mail"]。不过现在可以了,谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多