Showing posts with label Other - General C#. Show all posts
Showing posts with label Other - General C#. Show all posts

Round to Nearest 100,1000,10 Pow N

Here is a method which can round a value to nearest 100,1000, or any 10 Pow N.

This method follows the rounding rules –

private static int RoundToNDigits(double value,int digits) 
{
int valueAfterDecimal = Convert.ToInt32(value % Math.Pow(10, digits));
int valueBeforeDecimal = Convert.ToInt32(value) / Convert.ToInt32(Math.Pow(10, digits));
if (valueAfterDecimal < 50)
{
return valueBeforeDecimal * Convert.ToInt32(Math.Pow(10, digits));
}
else if (valueAfterDecimal > 50)
{

return Convert.ToInt32((valueBeforeDecimal + 1) * Math.Pow(10, digits));
}
else
{
if (valueBeforeDecimal % 2 == 0)
return (valueBeforeDecimal + 1) * Convert.ToInt32(Math.Pow(10, digits));
else
return valueBeforeDecimal * Convert.ToInt32(Math.Pow(10, digits));
}
}





 



 



Hope this helps



Cheers

Error obtaining group names. An operations error occurred.

The “Error obtaining group names. An operations error occurred.” exception tends to occur when using Active Directory :

The probably fix for this would be get the group names by explicitly passing the security context.

Here is some sample code-

public string GetGroups(string domainName, string userName, string password) 


        { 


            String domainAndUsername = domainName + @"\" + userName; 


            DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, password); 


            StringBuilder groupNames = new StringBuilder(); 


            try 


            {    //Bind to the native AdsObject to force authentication.            


                Object obj = entry.NativeObject; 


 


                DirectorySearcher search = new DirectorySearcher(entry); 


 


                search.Filter = "(SAMAccountName=" + userName + ")"; 


                search.PropertiesToLoad.Add("memberOf"); 


                SearchResult result = search.FindOne();


 


                int propertyCount = result.Properties["memberOf"].Count; 


 


                String dn; 


                int equalsIndex, commaIndex; 


 


                for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++) 


                { 


                    dn = (String)result.Properties["memberOf"][propertyCounter]; 


 


                    equalsIndex = dn.IndexOf("=", 1); 


                    commaIndex = dn.IndexOf(",", 1); 


                    if (-1 == equalsIndex) 


                    { 


                        return null; 


                    } 


 


                    groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1)); 


                    groupNames.Append("|"); 


 


                } 


 


            } 


            catch (Exception ex) 


            { 


                throw new Exception("Error authenticating user. " + ex.Message); 


            } 


            return groupNames.ToString(); 


        }


 




 



Hope this helps



Cheers