Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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());//UnCommented By Anil on Date 19 Jul 2010
				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....");

					//using(objDirectorySearcher = new DirectorySearcher())//Commented By Anil on date 16 Jul 2010 -1
					//{						//Commented By Anil on date 16 Jul 2010 -1
						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.PropertiesToLoad.Add("samAccountName"); //Commented By Anil on date 16 Jul 2010 -3
									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);
									}
								
								}//Added By Anil on Date 16 Jul 2010 -2
								//---- //Added By Anil on Date 19 Jul 2010 -4 ------
								
								if(objDirectorySearcher!=null)
								{
									objDirectorySearcher.Dispose();
								}
								if(objDirectoryEntry2!=null)
								{
									objDirectoryEntry2.Dispose();
								}
								//---- //Added By Anil on Date 19 Jul 2010 -4 ------
							}
						}
					//} //Commented By Anil on date 16 Jul 2010 -1
          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.");//UnCommented By Anil on Date 19 Jul 2010
				WriteLog("Date Uploaded successfully.");
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex.Message.ToString());//UnCommented By Anil on Date 19 Jul 2010
				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
Posted
Comments
LittleYellowBird 20-Jul-10 6:16am    
May I suggest that what you have included is not really a code snippet, it is too long. Most people will not have time to read and check all this code. It would proably help if you could narrow the problem down a bit more. Just a suggestion. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900