locked
How to add timestamp in specific columns of a DataTable? RRS feed

  • Question

  • User-1391672913 posted

    Hi,

    I have a program that puts csv file data in a DataTable. I have 9 columns in my DataTable, some of the columns are named "Add Date Time" and "Status Date Time". I am using TextFieldParser in inserting rows from the csv file to the DataTable. 

    for reference:

    using (TextFieldParser parser = new TextFieldParser(CSVFilePath))
                {
                    parser.HasFieldsEnclosedInQuotes = true;
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters("|");
    
                    while (!parser.EndOfData)
                    {
                        //Processing row
                        csvTable.Rows.Add();
                        int count = 0;
                        string[] fields = parser.ReadFields();
    
                        foreach (string field in fields)
                        {
                            csvTable.Rows[csvTable.Rows.Count - 1][count] = field;
    
                            count++;
                        }
                    }
                }
    

    Now, I need to add/append a timestamp in the cells of the columns "Add Date Time" and "Status Date Time" every time it gets inserted in the DataTable. Any Ideas How to do it? Thank you.

    Regards,

    NoobNoob

    Friday, March 6, 2020 6:40 AM

Answers

  • User-37275327 posted

    Change the column index and assign the timestamp, let's say indexes are 7 and 8 respectively,

     csvTable.Rows[csvTable.Rows.Count - 1][7] = DateTime.Now();
     csvTable.Rows[csvTable.Rows.Count - 1][8] = DateTime.Now();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 6, 2020 7:58 AM
  • User-1391672913 posted

    I have tried your advise but sadly, it overwrites the Data in the specific column. I just need to append a time when it is inserted.

    !!!UPDATE!!!

    I have tried your advise but with a little tweak. I made this instead:

    string timeStamp = DateTime.Now.ToString("HH:mm");
    
    foreach (string field in fields)
                        {
                            csvTable.Rows[csvTable.Rows.Count - 1][3] = field + " " + timeStamp;
                            csvTable.Rows[csvTable.Rows.Count - 1][7] = field + " " + timeStamp;
                            csvTable.Rows[csvTable.Rows.Count - 1][count] = field;
    
                            count++;
                        }

    Thank you for the advise!

    Sincerely, 

    NoobNoob

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 6, 2020 9:27 AM

All replies

  • User-37275327 posted

    Change the column index and assign the timestamp, let's say indexes are 7 and 8 respectively,

     csvTable.Rows[csvTable.Rows.Count - 1][7] = DateTime.Now();
     csvTable.Rows[csvTable.Rows.Count - 1][8] = DateTime.Now();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 6, 2020 7:58 AM
  • User-1391672913 posted

    I have tried your advise but sadly, it overwrites the Data in the specific column. I just need to append a time when it is inserted.

    !!!UPDATE!!!

    I have tried your advise but with a little tweak. I made this instead:

    string timeStamp = DateTime.Now.ToString("HH:mm");
    
    foreach (string field in fields)
                        {
                            csvTable.Rows[csvTable.Rows.Count - 1][3] = field + " " + timeStamp;
                            csvTable.Rows[csvTable.Rows.Count - 1][7] = field + " " + timeStamp;
                            csvTable.Rows[csvTable.Rows.Count - 1][count] = field;
    
                            count++;
                        }

    Thank you for the advise!

    Sincerely, 

    NoobNoob

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 6, 2020 9:27 AM