CSV to SQL - Add 1 day to a Date in a datacolumn

c# csv datacolumn datetime sqlbulkcopy

Question

I am trying to add 1 day to all dates that are in a certain datacolumn ['RecordAddedDate']

csvData.Columns.AddRange(new DataColumn[3] {                     
new DataColumn("Manufacturer", typeof(string)),
new DataColumn("SupplierCode", typeof(string)),
new DataColumn("RecordAddedDate", typeof(DateTime))});

At the moment the moment I have this working:

for (int rowIndex = 0; rowIndex < csvData.Rows.Count; rowIndex++)
{
DateTime dt2 = DateTime.Parse(fieldData[2]);
var newDate = dt2.AddDays(1);
csvData.Rows[rowIndex][2] = newDate;                                    
}

But it only adds 1 day to the first row read from the csv and doesn't add for the rest.

Any Help?

Here is the while loop which reads the data from the csv and adds the data

 while (!csvReader.EndOfData)
                {
                    string[] fieldData = csvReader.ReadFields();
                    //Making empty value as null
                    for (int i = 0; i < fieldData.Length; i++)
                    {
                        Console.WriteLine(fieldData[i]);
                        if (fieldData[i] == "")
                        {
                            fieldData[i] = null;
                        }

                        for (int rowIndex = 0; rowIndex < csvData.Rows.Count; rowIndex++)
                        {
                            DateTime dt2 = csvData.Rows[rowIndex].Field<DateTime>(2);
                            DateTime newDate = dt2.AddDays(1);
                            csvData.Rows[rowIndex][2] = newDate;
                        }                             

                    }
                    csvData.Rows.Add(fieldData);
                    Console.WriteLine("Rows count:" + csvData.Rows.Count);
                }
            }
        return csvData;

Popular Answer

What is fieldData[2]? You are always using this in the loop, so no wonder that you always get the same DateTime. If the table is already filled and you want to update a value use csvData.Rows[rowIndex][2] = csvData.Rows[rowIndex].Field<DateTime>(2).AddDays(1);

for (int rowIndex = 0; rowIndex < csvData.Rows.Count; rowIndex++)
{
    DateTime dt2 = csvData.Rows[rowIndex].Field<DateTime>(2);
    DateTime newDate = dt2.AddDays(1);
    csvData.Rows[rowIndex][2] = newDate;                                    
}


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why