Dear All,
I have developed an windows application in .net1.1 using c# code to fetch all users' information from Active Directory and insert into oracle DB. It is working fine for less than 4000 records. For more than 4000 records it throws the "System.OutOfMemoryException: Exception of type System.OutOfMemoryException was thrown. " exception. Code snipt given below.
public void GetEmployeeData()
{
DirectorySearcher objDirectorySearcher =null;
DirectoryEntry objDirectoryEntry1=null;
WTODbConnect objConn=null;
long _systemid;
string _path=string.Empty;
string _uid=string.Empty;
string _pwd=string.Empty;
string strMailID="";
string strLANID="";
string StrFirstName="";
string StrLastName="";
string strSql="";
long rfval=0;
string CurrDateTime="";
try
{
_systemid=Convert.ToInt64(System.Configuration.ConfigurationSettings.AppSettings["systemid"]);
Console.WriteLine("Searching & Loading start at........." + DateTime.Now.ToString());
using(objConn = new WTODbConnect(Convert.ToInt64(_systemid)))
{
_strLogPath=Convert.ToString(objConn.ResultScaler("select C_PAR_VALUE1 from PD_PARAM_MST where C_PAR_CODE='ACTIVE_DIRECTORY' and C_PAR_SUBCODE ='LOGFILEPATH'"));
_path=Convert.ToString(objConn.ResultScaler("select C_PAR_VALUE1 from PD_PARAM_MST where C_PAR_CODE='ACTIVE_DIRECTORY' and C_PAR_SUBCODE ='SERVER_USERID_PWD'"));
_uid=Convert.ToString(objConn.ResultScaler("select C_PAR_VALUE2 from PD_PARAM_MST where C_PAR_CODE='ACTIVE_DIRECTORY' and C_PAR_SUBCODE ='SERVER_USERID_PWD'"));
_pwd=Convert.ToString(objConn.ResultScaler("select C_PAR_VALUE3 from PD_PARAM_MST where C_PAR_CODE='ACTIVE_DIRECTORY' and C_PAR_SUBCODE ='SERVER_USERID_PWD'"));
WriteLog("Scheduler Started and fetching Active Directory Server Information....");
strSql="TRUNCATE TABLE PD_AD_DATA_FETCH_TEMP";
objConn.executeDML(strSql,ref rfval);
strSql=string.Empty;
strSql="UPDATE PD_PARAM_MST SET C_PAR_VALUE1='N' WHERE C_PAR_CODE='ACTIVE_DIRECTORY' AND C_PAR_SUBCODE='DATA_FETCH_FLAG'";
objConn.executeDML(strSql,ref rfval);
WriteLog("Flag set to 'N' in parameter master table.");
using(objDirectoryEntry1 = new DirectoryEntry(_path,_uid,_pwd,AuthenticationTypes.Secure))
{
WriteLog("Authenticating Connection and Fetching Data from Active Directory Server....");
foreach(DirectoryEntry objDirectoryEntry2 in objDirectoryEntry1.Children)
{
using(objDirectorySearcher = new DirectorySearcher())
{
objDirectorySearcher.SearchRoot = objDirectoryEntry2;
objDirectorySearcher.Filter = "(&(objectCategory=user)(objectClass=user)(givenName=*)(sn=*)(mail=*)(samAccountName=*))";
objDirectorySearcher.PropertiesToLoad.Add("displayname");
objDirectorySearcher.PropertiesToLoad.Add("proxyaddresses");
objDirectorySearcher.PropertiesToLoad.Add("samAccountName");
objDirectorySearcher.PropertiesToLoad.Add("givenName");
objDirectorySearcher.PropertiesToLoad.Add("sn");
objDirectorySearcher.PropertiesToLoad.Add("mail");
objDirectorySearcher.CacheResults = false;
objDirectorySearcher.SearchScope = SearchScope.Subtree;
foreach(SearchResult objSearchResult in objDirectorySearcher.FindAll())
{
if(!(Convert.ToString(objSearchResult.GetDirectoryEntry().Properties["givenName"].Value)==string.Empty) )
{
StrFirstName = objSearchResult.GetDirectoryEntry().Properties["givenName"].Value.ToString();
StrFirstName = StrFirstName.Replace("'", "''");
}
if(!(Convert.ToString(objSearchResult.GetDirectoryEntry().Properties["sn"].Value)==string.Empty) )
{
StrLastName = objSearchResult.GetDirectoryEntry().Properties["sn"].Value.ToString();
StrLastName = StrLastName.Replace("'", "''");
}
if(!(Convert.ToString(objSearchResult.GetDirectoryEntry().Properties["samAccountName"].Value)==string.Empty) )
{
strLANID = objSearchResult.GetDirectoryEntry().Properties["samAccountName"].Value.ToString();
strLANID = strLANID.Replace("'", "''");
}
if(!(Convert.ToString(objSearchResult.GetDirectoryEntry().Properties["mail"].Value)==string.Empty) )
{
strMailID = objSearchResult.GetDirectoryEntry().Properties["mail"].Value.ToString();
strMailID = strMailID.Replace("'", "''");
}
CurrDateTime = DateTime.Now.ToString();
strSql=string.Empty;
strSql = "INSERT INTO PD_AD_DATA_FETCH_TEMP ( C_LAN_ID, C_FIRST_NAME, C_LAST_NAME, C_EMAIL_ADD, DT_DATE) VALUES ('" + strLANID + "','" + StrFirstName + "','" + StrLastName + "','" + strMailID + "','" + CurrDateTime + "')";
objConn.executeDML(strSql,ref rfval);
}
}
if(objDirectorySearcher!=null)
{
objDirectorySearcher.Dispose();
}
if(objDirectoryEntry2!=null)
{
objDirectoryEntry2.Dispose();
}
}
}
WriteLog("Data Fetching from Active Directory Server Completed.");
strSql=string.Empty;
strSql="UPDATE PD_PARAM_MST SET C_PAR_VALUE1='Y' WHERE C_PAR_CODE='ACTIVE_DIRECTORY' AND C_PAR_SUBCODE='DATA_FETCH_FLAG'";
objConn.executeDML(strSql,ref rfval);
WriteLog("Flag set to 'Y' in parameter master table.");
}
Console.WriteLine("Date Uploaded successfully.");
WriteLog("Date Uploaded successfully.");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
WriteLog("<font color=red>"+ex.InnerException.Source.ToString() + "<br>"+ex.InnerException.StackTrace.ToString()+"</font>");
}
finally
{
if(objConn !=null && objConn.IsConnected==true)
{
objConn.CloseConnection();
objConn.Dispose();
}
else if(objConn !=null)
{
objConn.Dispose();
}
if(objDirectorySearcher!=null)
{
objDirectorySearcher.Dispose();
}
if(objDirectoryEntry1!=null)
{
objDirectoryEntry1.Dispose();
}
}
}
Please help me to resolve OutOfMemoryException issue and I can update all existing users' infromation from AD to Oracle DB. I would be appriciate.
Thanks in advance.
Best Regards, Anil