Here is my datatable:
DataTable CSVFile = new DataTable();
CSVFile.Columns.Add("Occurrence_Date", typeof(DateTime));
CSVFile.Columns.Add("Preanalytical_Before_Testing", typeof(string));
CSVFile.Columns.Add("Cup_Type", typeof(string));
CSVFile.Columns.Add("Analytical_Testing_Phase", typeof(string));
CSVFile.Columns.Add("Area", typeof(string));
CSVFile.Columns.Add("Postanalytical_After_Testing", typeof(string));
CSVFile.Columns.Add("Other", typeof(string));
CSVFile.Columns.Add("Practice_Code", typeof(string));
CSVFile.Columns.Add("Comments", typeof(string));
I am trying to add a new row to it:
DataRow newRow = CSVFile.NewRow();
newRow["Occurrence_Date"] = Convert.ToDateTime(splitcsvlines[GetColumnsNames.Occurrence_Date]);
newRow["Preanalytical_Before_Testing"] = splitcsvlines[GetColumnsNames.Preanalytical_Before_Testing];
newRow["Cup_Type"] = splitcsvlines[GetColumnsNames.Cup_Type];
newRow["Analytical_Testing_Phase"] = splitcsvlines[GetColumnsNames.Analytical_Testing_Phase];
newRow["Area"] = splitcsvlines[GetColumnsNames.Area];
newRow["Postanalytical_After_Testing"] = splitcsvlines[GetColumnsNames.Postanalytical_After_Testing];
newRow["Other"] = splitcsvlines[GetColumnsNames.Other];
newRow["Practice_Code"] = splitcsvlines[GetColumnsNames.Practice_Code];
newRow["Comments"] = splitcsvlines[GetColumnsNames.Comments];
CSVFile.Rows.Add(newRow);
However I am getting an error
The type initializer for ‘BulkUploadToLOMDatabase.GetColumnsNames’ threw an exception.`
I have tried to just do newRow["Cup_Type"] = "something" or newRow[2] = "something" and still getting the same error
Here is what the class looks like:
class GetColumnsNames
{
public static int Occurrence_Date = Convert.ToInt16(ConfigurationSettings.AppSettings["Occurrence_Date"].ToString());
public static int Preanalytical_Before_Testing = Convert.ToInt16(ConfigurationSettings.AppSettings["1_0_Preanalytical_Before_Testing"].ToString());
public static int Cup_Type = Convert.ToInt16(ConfigurationSettings.AppSettings["Cup_Type"].ToString());
public static int Analytical_Testing_Phase = Convert.ToInt16(ConfigurationSettings.AppSettings["Analytical_Testing_Phase"].ToString());
public static int Area = Convert.ToInt16(ConfigurationSettings.AppSettings["Area"].ToString());
public static int Postanalytical_After_Testing = Convert.ToInt16(ConfigurationSettings.AppSettings["Postanalytical_After_Testing"].ToString());
public static int Other = Convert.ToInt16(ConfigurationSettings.AppSettings["Other"].ToString());
public static int Practice_Code = Convert.ToInt16(ConfigurationSettings.AppSettings["Practice_Code"].ToString());
public static int Comments = Convert.ToInt16(ConfigurationSettings.AppSettings["Comments"].ToString());
}
What am i doing wrong? What is wrong with my class?
The type initializer for BulkUploadToLOMDatabase.GetColumnsNames' threw an exception.This means that the CLR tried create your type but it failed. Since you have a bunch of static variables that rely on a call that could fail wth
KeyNotFoundFormatExecptionNullExceptionyou should convert this class to a singleton instead.This way you can properly catch any problems with ConfigurationSettings.AppSettings
As an aside you might want to consider using system.configuration.configurationmanager instead. That is unless you’re in 1.1. ConfigurationSettings.AppSettings went obsolete in 2.0