Error : Column 'Employee_Key' does not belong to table Employee.
Hi,
I am trying to popluate a datarow (please the attached code) with values from Active Directory and i get the error 'Column 'Employee_Key' does not belong to table Employee' on line dr["Employee_Key"] = dey.Properties["title"].Value ?? string.Empty;
All i am trying to do is load a schema of the Employee table from the database and apply it to dt (datatable) and then loop through all the rows of Active Directory and built a table with that data.
however when i create a new row and try to assign values i get the above error
Please, could you help me figure out why i am getting this error... I searched in google but of no avail :'( I'm on a dead end :((
public DataTable GetData(Entities.Config.EtlConfig config) { var s = new SqlLoader(); var dt = s.GetSchemaTable(config.SqlTableToLoad); string setdate = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"); dt.TableName = "Employee"; try { DirectoryEntry de = GetDirectoryEntry(); DirectorySearcher ds = new DirectorySearcher(de); SearchResultCollection results = ds.FindAll(); foreach (SearchResult result in results) { DirectoryEntry dey = GetDirectoryEntry(); DataRow dr = dt.NewRow(); dr["Employee_Key"] = dey.Properties["title"].Value ?? string.Empty; dr["Employee_Name"] = dey.Properties["cn"].Value; dr["Employee_Full_Name"] = dey.Properties["distinguishedName"].Value; dr["Employee_Phone_Number"] = dey.Properties["telephoneNumber"]; dr["Email_Address"] = dey.Properties["mail"]; dr["Business_Unit"] = dey.Properties["department"]; dr["Supervisor_Full_Name"] = dey.Properties["manager"]; dr["Row_Update_Date"] = setdate; dr["Source"] = config.Source; dt.Rows.Add(dr); dey.Close(); } de.Close(); return dt; } catch(Exception e) { _log.Error(e.Message); throw new CustomException(e,ErrorType.DataImporter,e.Message); } } public DataTable GetSchemaTable(string sqlTable) { DataTable dt; try { _dbManager.Open(); SqlDataReader rd; using (rd= (SqlDataReader)_dbManager.ExecuteReader(CommandType.Text, "Select top 1 * from " + sqlTable + " NOLOCK")) { dt = rd.GetSchemaTable(); } } catch (Exception ex) { _log.Error("Error getting schema for table : " + sqlTable + " " + ex.Message); throw new CustomException(ex,ErrorType.SQLImport,"Error getting schema for table : " + sqlTable + " " + ex.Message); } finally { _dbManager.Dispose(); } return dt; }
>>>i get the error 'Column 'Employee_Key' does not belong to table Employee'
This might sound obvious but does the table Employee have a column (field) called Employee_Key ? (Check the spelling)
mrichmon
I think that it is not seeing that as a column name. Before you try accessing the column try this:
foreach(DataColumn column in dt.Columns.)
{
Response.Write(column.ColumnName);
}
Then see what you get...
Nate Feinberg
This may or may not have any relevance to your error (though I'm thinking it probably does), but where is variable _dbmanager initialized, if it is at all? If it's not done properly, stuff like this can happen. May also want to check out your SqlLoader definition (unless I'm just not seeing it in the MSDN for some reason).
Definitely try mrichmon's code, and poste the results (if any) unless they lead you to a direct conclusion and you no longer need our assistance :P
SqlConnection conn = new SqlConnection(ConfigManager.DbConnection());
SqlCommand cmd = new SqlCommand("Select top 0 * from " + sqlTable, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(table);
conn.Close();
This might sound obvious but does the table Employee have a column (field) called Employee_Key ? (Check the spelling)