How to add a text file in to existing datatable as a new column

RAMAN RAGAVAN 51 Reputation points
2021-12-14T21:37:41.407+00:00

I have to add a data column in to the existing data table.

I need to add one more column and data to that table from a .txt file in to 0th column and from second column i need to keep the DB data. I am not using any grid view and stream reader to my app.Need to use File.ReadAllLines();

my .txt file is like

NAME
val1
val3
val5

I have written a code that will add data to the first column row but after addedd my pgm suddenly stoped.Its not returning my table after this .Some issue with the loop i think.I am not sure.. please help me on this

My code..

   adapter.Fill(table);//some db data clms inside the table

 if (tablename == "TestData")
  {
var pathC = @"H:\claimdetails\claims\names.txt";

string[] result = File.ReadAllLines(pathC);

  DataColumn Col = table.Columns.Add("Name", typeof(String));

  Col.SetOrdinal(0); // set column to first position

      i=1;
    foreach (DataRow row in table.Rows)
     {
      row["Name"] = result[i];
      i++;
      }

    }
 return table; // not coming here

After this foreach suddenly program stops and not returning the table

Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Nasreen Akter 10,811 Reputation points Volunteer Moderator
    2021-12-14T22:07:59.237+00:00

    Hi @RAMAN RAGAVAN ,

    Thank you for the ask.

    I think you just need a if condition to check the array length + start the index from 0 instead of 1. Please see the code below. Thanks!

                var pathC = @"H:\claimdetails\claims\names.txt";  
                string[] result = File.ReadAllLines(pathC);  
                DataColumn Col = table.Columns.Add("Name", typeof(String));  
                Col.SetOrdinal(0); // set column to first position  
                i = 0;  
                foreach (DataRow row in table.Rows)  
                {  
                    if (i < result.Length)  
                    {  
                        row["Name"] = result[i];  
                        i++;  
                    }  
                }
    
    1 person found this answer helpful.

  2. Jack J Jun 25,296 Reputation points
    2021-12-17T06:25:56.993+00:00

    @RAMAN RAGAVAN ,

    I have one doubt and this problem occured.What if the table row count is less and Name colom count is big?

    If you worry about the Name column has a big count and table row count is less than the Name row count, I suggest that you can add some empty row by comparing the row count.

    Here is a code example you could refer to.

      static void Main(string[] args)  
            {  
                DataTable table = new DataTable();  
                table.TableName="test1";  
                table.Columns.Add("Age", typeof(int));  
                table.Columns.Add("Id", typeof(int));  
                table.Rows.Add(23,1001);  
                table.Rows.Add(24, 1002);  
                table.Rows.Add(25, 1003);  
                if(table.TableName=="test1")  
                {  
                    string[] str = File.ReadAllLines("D:\\1.txt");  
                    table.Columns.Add(str[0],typeof(string)).SetOrdinal(1);  
      
                    int count = str.Count()-1-table.Rows.Count;  
                    if(count>0)  
                    {  
                        for (int i = 0; i < count; i++)  
                        {  
                            table.Rows.Add();  
                        }  
                         
                    }  
                    for (int i = 0; i < table.Rows.Count; i++)  
                    {  
                          
                        table.Rows[i][str[0]]=str[i+1];  
                    }  
      
                    Console.WriteLine("success");  
                }  
                      
      
                  
      
            }  
    

    Result:

    158417-image.png

    As we can see from the following link, we can see the Name column row's count is more than the initial Datatable row's count.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.