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 8252319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:31:54+00:00 2026-06-08T00:31:54+00:00

My requirement is I need to split a text file and store into a

  • 0

My requirement is I need to split a text file and store into a datatable. Refer
the following code and column separator “|” and row separator “^”:

var text = streamReader.ReadToEnd();
var lines = text.Split('^');
var rows = lines.Select(l => new {Line = l, Fields = l.Split('|')});
var colCount = rows.Max(r => r.Fields.Length);

var tblRegistration = new DataTable();
for (int i = 1; i <= colCount; i++)
{
    tblRegistration.Columns.Add("Column" + i, typeof (string));
}

When it is correct format it will work fine.

But end user may upload text file like

adfafdsafsdfsdfs^fsdf|sfsdf|sdfsfd|dfs...

in the first row there is only one column, but there should be four.
In this case how to validate it?

  • 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-06-08T00:31:56+00:00Added an answer on June 8, 2026 at 12:31 am

    If your desired result is that you fill every column from left to the right it’s easy:

    foreach (var row in rows)
    {
        var newRow = tblRegistration.Rows.Add();
        newRow.ItemArray = row.Fields;
    }
    

    So your example file would generate this DataTable:

    Column1            Column2   Column3    Column4
    adfafdsafsdfsdfs            
    fsdf               sfsdf     sdfsfd     dfs
    

    If you want to throw an exception:

    foreach (var row in rows)
    {
        if(row.Fields.Length != tblRegistration.Columns.Count)
            throw new Exception("Please upload the correct format");
    
        var newRow = tblRegistration.Rows.Add();
        newRow.ItemArray = row.Fields;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All,In my requirement need to send a mail with attached text file, which is
Wondering if I could get some advice and direction on this following requirement: Need
I have a requirement where i need to enter a text in the text
Trying to determine best way to code this requirement : Need to insert rows
As per project requirement we need to convert images from word document into bitmap
I am wondering how will i proceed with the following requirement. I need to
I need to split an expression with the following strings: 'with', 'select', 'from', 'where'
I have a xml file. As per my requirement I need to update empty
New to twitter api. Requirement: Need to send direct messages to twitter followers of
Requirement: I need to run multiple animations one by one. I'm using onAnimationEnd() to

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.