【发布时间】: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