开发者

Trouble adding new rows into a DataTable

开发者 https://www.devze.com 2023-03-20 07:17 出处:网络
Here is my datatable: DataTable CSVFile = new DataTable(); CSVFile.Columns.Add(\"Occurrence_Date\", typeof(DateTime));

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.ToD开发者_运维知识库ateTime(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 KeyNotFound FormatExecption NullException you 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


Make sure ConfigurationSettings.AppSettings has those integer values you are asking for. It looks like that is throwing the error.

Your datatable and datarow code is fine.


I think that the problem appears because of this line of code:

public static int Occurrence_Date = Convert.ToInt16(ConfigurationSettings.AppSettings["Occurrence_Date"].ToString());

int32 values cannot be converted to DateTime values:

newRow["Occurrence_Date"] =  Convert.ToDateTime(splitcsvlines[GetColumnsNames.Occurrence_Date]);

As @LarsTech told, your dataTable related code is correct. The most dangerous is the code I've posted above. Please implement it in a simple console where this (ConfigurationSettings.AppSettings["Occurrence_Date"].ToString() )particular value is converted to int15 and then to DateTime. What are your results?


As other have mentioned, building/loading the table is fine... here is quick test, using ordinal position versus column names to load 10 random values. You said in your comments the debug is showing the right value(s), if so then everything should be working. Just "make sure" you are getting the expected data type for each column value.

        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));

        for (int i = 0; i < 10; i++)
        {
            DataRow row = CSVFile.NewRow();
            row[0] = DateTime.Now.AddDays(i);
            row[1] = "Field 2";
            row[2] = "Field 3";
            row[3] = "Field 4";
            row[4] = "Field 5";
            row[5] = "Field 6";
            row[6] = "Field 7";
            row[7] = "Field 8";
            row[8] = "Field 9";

            CSVFile.Rows.Add(row);
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消