Friday, 4 January 2013

Read list values & store temporarily in Table structure to perform operation


DataTable demoTable;

/// Function to define table structure

demoTable = CreateTableStructure();
foreach (SearchResult result in searcher.FindAll())
{
                UserDisplayName = GetProperty(result, EmpDisplayName);
Designation = GetProperty(result, EmpDesignation);
Department = GetProperty(result, EmpDepartment);
EMail = GetProperty(result, EmpEMail);

/// Create rows in the table & insert values in every column.

DataRow row1 = demoTable.NewRow();
row1["userdisplayname"] = UserDisplayName;
row1["designation"] = Designation;
row1["department"] = Department;
row1["email"] = EMail;
demoTable.Rows.Add(row1);
}
/// Function to read values from data table one by one & perform further operations

  UpdateList(demoTable);

/// Function to read value of a specified property from Active Directory
private string GetProperty(SearchResult searchResult, string PropertyName)
{
if (searchResult.Properties.Contains(PropertyName))
{
return searchResult.Properties[PropertyName][0].ToString();
}
else
{
return string.Empty;
}
}

/// Function to create table structure

public DataTable CreateTableStructure ()
{
            DataTable demotab = new DataTable();

            DataColumn displayname = new DataColumn("userdisplayname");
            DataColumn designation = new DataColumn("designation");
            DataColumn department = new DataColumn("department");
            DataColumn email = new DataColumn("email");

            displayname.DataType = System.Type.GetType("System.String");
            designation.DataType = System.Type.GetType("System.String");
            department.DataType = System.Type.GetType("System.String");
            email.DataType = System.Type.GetType("System.String");

            demotab.Columns.Add(displayname);
            demotab.Columns.Add(designation);
            demotab.Columns.Add(department);
            demotab.Columns.Add(email);
           
            return demotab;
}

/// Function to perform Operations on List by reading each row of table

public void UpdateList (DataTable demoTable)
{
string UserDisplayName;
string Designation;
string Department;
string EMail;

if (demoTable.Rows.Count > 0)
{
for (int i = 0; i <= demoTable.Rows.Count - 1; i++)
{
UserDisplayName = demoTable.Rows[i]["userdisplayname"].ToString();
Designation = demoTable.Rows[i]["designation"].ToString();
Department = demoTable.Rows[i]["department"].ToString();
EMail = demoTable.Rows[i]["email"].ToString();
                   
//Perform List Update Operation
                                       
}
}
}

No comments:

Post a Comment