Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6887245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:51:41+00:00 2026-05-27T05:51:41+00:00

I have stored values retrieved from a text file into an array. Now am

  • 0

I have stored values retrieved from a text file into an array. Now am trying to store those array values into a data table with the below code.

The problem is that if the first row has 20 values and second row has 23 values, then 43 cells are created in data table of which last 20 (1st row column count) are empty. This continues for each row. Is there any other way to store the array into a data table?

var reader = new StreamReader(File.OpenRead(@"d:\er.txt"));
var table = new DataTable("SampleTable");

while (!reader.EndOfStream)
{
    var line = reader.ReadLine().Trim();
     var values = line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
    string[] ee = values;
    var newRow = table.NewRow();
    for (int i = 0; i < ee.Length; i++)
    {
        table.Columns.Add();
        newRow[i] = ee[i].Trim().ToString(); // like sample [0,0]
    }
    table.Rows.Add(newRow);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:51:42+00:00Added an answer on May 27, 2026 at 5:51 am

    This is because you’re adding a column for each value that you add to the table. You only need to add another column if it’s required. Something like the following should suffice:

    var reader = new StreamReader(File.OpenRead(@"d:\er.txt"));
    var table = new DataTable("SampleTable");
    int columnCount = 0;
    while (!reader.EndOfStream)
    {
      var line = reader.ReadLine().Trim();
      var values = line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
      string[] ee = values;
      while (columnCount < ee.Length) 
      {
        // Only add a new column if it's required.
        table.Columns.Add();
        columnCount++;
      }
      var newRow = table.NewRow();
      for (int i = 0; i < ee.Length; i++)
      {
          newRow[i] = ee[i].Trim().ToString(); // like sample [0,0]
      }
      table.Rows.Add(newRow);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that returns values from a temp table. In my
I am trying to retrieve values from my App.config file which is stored in
I have some financial values stored as text in a mysql db. the significance
I have classname stored in variable $classname; also I have an array of values
I am trying to retrieve particular data from a txt file and would like
I have stored a list of items from gridView into the registry as below:
I am trying to retrieve data from a file to change it and replace
I have stored values in NsMutableDictionaries . ThenI stored all the dictionaries in NSMutable
I have values stored as strings in a DataTable where each value could really
I have a stored procedure that returns two int Values. The first can not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.