locked
add value to datatable column RRS feed

  • Question

  • User-2051535749 posted

    I'm creating a datatable from a CSV file. However, I need to pre populate a column with today's date. When I try to do it, I get an error, [column 1 not found]. How can I create my table and pre populate a column with today's date in the data table column? I need to populate the TradeCreated Column with today's date.

    code:

     DataTable dt = new DataTable();
                dt.Columns.Add("TradeId");
                dt.Columns.Add("TradeCreated");
                string readIt = File.ReadAllText(fileName);
    
                using (StreamReader sr = new StreamReader(fileName))
                {
                  
                    while ((line = sr.ReadLine()) != null)
                    {
                         
                        if (!string.IsNullOrEmpty(line))
                        {
                            dt.Rows.Add();
                            
                            int count = 0;
                            foreach (string trades in line.Split(','))
                            {
                                dt.Rows[dt.Rows.Count - 1][count] = trades;
                                count++;
                            }
                        }
    
                    }
                }

    Monday, January 7, 2019 4:52 PM

Answers

  • User475983607 posted

    Try this...

    DataTable dt = new DataTable();
    dt.Columns.Add("TradeId");
    dt.Columns.Add("TradeCreated");
    dt.Columns.Add("TheDate");
    string readIt = File.ReadAllText(fileName);
    
    using (StreamReader sr = new StreamReader(fileName))
    {
      
    	while ((line = sr.ReadLine()) != null)
    	{
    		 
    		if (!string.IsNullOrEmpty(line))
    		{
    			dt.Rows.Add();
    			
    			int count = 0;
    			foreach (string trades in line.Split(','))
    			{
    				dt.Rows[dt.Rows.Count - 1][count] = trades;
    				count++;
    			}
    			dt.Rows[dt.Rows.Count - 1][count] = Datetime.Now.ToString();
    		}
    
    	}
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 7, 2019 5:23 PM

All replies

  • User475983607 posted

    Your code has bugs and throws exceptions!  Please do not post code that does not work or compile! 

    Simply add a column and add the date.

    dt.Columns.Add("TheDate", typeof(DateTime));
    DataRow dr = dt.NewRow();
    dr["TheDate"] = DateTime.Now;
    

    https://stackoverflow.com/questions/3125864/adding-rows-to-dataset

    Monday, January 7, 2019 5:15 PM
  • User-2051535749 posted

    My code does compile and run fine, your code snippet does not add the date to the datatable column

    Monday, January 7, 2019 5:18 PM
  • User475983607 posted

    Try this...

    DataTable dt = new DataTable();
    dt.Columns.Add("TradeId");
    dt.Columns.Add("TradeCreated");
    dt.Columns.Add("TheDate");
    string readIt = File.ReadAllText(fileName);
    
    using (StreamReader sr = new StreamReader(fileName))
    {
      
    	while ((line = sr.ReadLine()) != null)
    	{
    		 
    		if (!string.IsNullOrEmpty(line))
    		{
    			dt.Rows.Add();
    			
    			int count = 0;
    			foreach (string trades in line.Split(','))
    			{
    				dt.Rows[dt.Rows.Count - 1][count] = trades;
    				count++;
    			}
    			dt.Rows[dt.Rows.Count - 1][count] = Datetime.Now.ToString();
    		}
    
    	}
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 7, 2019 5:23 PM
  • User-2051535749 posted

    I had something similar just in the wrong location within the code, this works, thanks

    Monday, January 7, 2019 5:30 PM