Posts Tagged ‘MS.NET’

WCF and MSMQ technical links on MSDN

March 9, 2010 Leave a comment


MSMQ under Networking header:

Categories: WCF Tags:

Insert Distinct Records into ArrayList

February 24, 2010 Leave a comment

Many a times we people come across a requirement where we need to find/extract/query or push/insert/add distinct records and or items into collections like Array, ArrayList, IList. So the same happened with me recently where I wanted to maintain distinct records into the arraylist object which is shared across the layers.
To suffice this requirement I tried lot many approaches and solutions (some very traditional yet time consuming, some hi-fi but too complex to implement 🙂 ) even I searched a lot for the same but could not find any handy solution. Thats the time where I came across “BinarySearch” method under ArrayList class.
I did bit analysis and then made tiny changes into my code to leverage the benifits of BinarySearch method. So becaue of this my code looked as below:

// push the metadata of Master tables by refering the MasterFieldsDataTable
for (int listIndex = 0; listIndex < MasterFieldsDetails.Count; listIndex++)
for (int rowIndex = 0; rowIndex < MasterTablesDT.Rows.Count; rowIndex++)
if (MasterTablesDT.Rows[rowIndex][“Description”].ToString().ToUpper().Equals(MasterFieldsDetails[listIndex].Discription.ToUpper()))
// processed some data here and some manipulation…..

// Add the item to Arraylist is its count is zero
if (0 == uniqueTableNames.Count)
// Do the BinarySearch for Item
int itemfound = uniqueTableNames.BinarySearch(MasterFieldsDetails[listIndex].TableName);
// If item is not found then add it to the ArrayList
if (0 > itemfound)
// Sort the Arraylist for FoolProof BinarySearch in Next Iteration


Now in above code there is 1 catch and that is because the iteration is been broken prematurely which some how affects the indexing of arraylist items. Now because of this the BinarySearch donot work and allows to add duplicate/redundent data to be added to it. So to avoid this we need to sort the arraylist.
More details on the ArrayList class and its methods can be found here:
Well this is all about my observation and approach i decided to solve the issue. Any idea, suggestion, solution will be appreciated. Similarly more details, explaination and clarity on the said issue is also appreciated.

Categories: General Tags:

Get all active directory users using .NET

November 25, 2009 Leave a comment

Few days back I had requirment for pulling all the active directory users and it into a list. I tried few nice codes from online help but they failed for pulling all the users from AD, also the users I expected from the LDAP query (Active Directory) were not right (seems my domain and ldap path wasn’t right at all).
So finally I did some R n D also taken help from Network Admin for right LDAP path and domain name 🙂 . After that everything started working well. Following is the code for same.

public DataTable GetActiveDirectoryUsers()

            string ldapPath = ConfigurationManager.AppSettings[“LDAP”]; // e.g “LDAP://OU=ADSI,DC=ds,DC=microsoft,DC=com
            string domainPath = ConfigurationManager.AppSettings[“DomainName”]; // not needed anymore 
                //Initiate directoryEntry object here by specifying the LDAP Path.
                DirectoryEntry directoryEntry = new DirectoryEntry(ldapName);
                // Create Directory Searcher object for firing search on the LDAP
                DirectorySearcher directorySearcher = new DirectorySearcher();
                // no need to define the datacolumns for the datatable here as it is typed datatable
                DataRow newUsersRow;
                {   // Set directorySearcher attributes here
                    directorySearcher.SearchRoot = directoryEntry;
                    directorySearcher.SearchScope = SearchScope.Subtree;
                    // Set The Fields/ColumnNames to be fetched
                    //define filter criteria for search here
                    directorySearcher.Filter = “(&(objectCategory=person)(objectClass=User)(givenname=*))”;
                    // Set the pagesize in order to get all records here
                    directorySearcher.PageSize = 1000;

                    // Fire the search query here and collect the results in SearchResultCollection object
                    SearchResultCollection resultCollection = directorySearcher.FindAll();

                            foreach (SearchResult result in resultCollection)
                                newUsersRow = activeDirectoryDT.NewUsersRow();
                                newUsersRow.UserID = –currentUserID;

                                if (!((result.Properties[“sAMAccountName”][0]).ToString().ToUpper().StartsWith(domainName + “\\”)))
                                    newUsersRow.UserName = domainName + “\\” + (result.Properties[“sAMAccountName”][0]).ToString();
                                    newUsersRow.UserName = (result.Properties[“sAMAccountName”][0]).ToString();

                                newUsersRow.AliasName = (result.Properties[“name”][0]).ToString();
                                    newUsersRow.Email = (result.Properties[“mail”][0]).ToString();
                                newUsersRow.groupid = 1;
                                newUsersRow.Active = false;
                                newUsersRow.CreatedBy = 0;
                                newUsersRow.CreatedDate = DateTime.Now;
                                newUsersRow.UserType = “EndUser”;
                                newUsersRow.UserTypeID = 3;

                        catch (Exception ex)
                            // do something here
                catch (Exception ex)
                  // do something here

            return activeDirectoryDT;               

In the above code few things are very important to note:

  1. LDAP path e.g “LDAP://OU=ADSI,DC=ds,DC=microsoft,DC=com” its very precise path for getting right DOMAIN
  2. Search Root and Search Scope sequnce must be as above, also the they must be set before listing the field name/column names that you want to pull out
  3. The Search Filter must be set after listing the field name/column names that you want to pull out
  4. The Page Size  must be set after search filter is set
  5. The Page Size Property helps you to pull out all of the Users. If this is not set then you will not get all users. This property is very similar to the Paging property of gridview which allows us to implement paging of the pulled data (here it does internal paging of the users and then returns the final composite users list)

I hope this will be helpful for you 🙂