Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I face a problem when i read text Files. what i did is i get certain details from the user Webform. i used to get customer Address via "MULTILINE" Textbox.
EX:

NO.100,
BLOCK 75/A,
Nelson Road,
New York,
USA.

Then after generating Text File i used to read data from particular text file and add those data into the database.(EDI purpose).But when i used perticuler type of address the EDi did not reads feilds after the address feild and it returns NULL values
EX:


C#
parameters[8] = BAL.CSV_temp.newTable1.Rows[i].ItemArray[5].ToString();
                    parameters[9] = BAL.CSV_temp.newTable1.Rows[i].ItemArray[6].ToString();
                    parameters[10] = common.selectCountry(BAL.CSV_temp.newTable1.Rows[i].ItemArray[7].ToString());
                    parameters[11] = common.selectDefaultCustomer();
                    parameters[12] = BAL.CSV_temp.newTable1.Rows[i].ItemArray[8].ToString();
                    parameters[13] = BAL.CSV_temp.newTable1.Rows[i].ItemArray[9].ToString();
                    parameters[14] = BAL.CSV_temp.newTable1.Rows[i].ItemArray[10].ToString();
                    parameters[15] = BAL.CSV_temp.newTable1.Rows[i].ItemArray[11].ToString();





But when i put the Address as a following way(In single line) it reads perfectly from the Text File.


NO.100,BLOCK 75/A,Nelson Road,New York, USA.


Does anyone have a idea to solve this problem. if there any way to CONVERT that MULTILINE text to a single line as mentioned above? thanks in advance!!!
Posted
Updated 1-Aug-12 22:47pm
v2

Look at string.Join:
C#
string[] lines = { "line1,", "line2,", "line3," };
string s = string.Join("", lines);
 
Share this answer
 
C#
string txt = txtMulti.Text.Replace(Environment.NewLine, " ");//one empty space
MessageBox.Show(txt);
 
Share this answer
 
If u want to read from .txt file then below code will help

C#
public static DataTable exceldata(string filePath)
        {     
         
        DataTable dtexcel = new DataTable();
       var filename = filePath;
                 var reader = ReadAsLines(filename);
                 // var data = new DataTable();
                 //This assume the first record is filled with the column names
                 var headers = reader.First().Split('\t');
                 foreach (var header in headers)
                 {
                     dtexcel.Columns.Add(header);
                 }
                 var records = reader.Skip(1);
                 foreach (var record in records)
                 {
                     dtexcel.Rows.Add(record.Split('\t'));
                 }
}



XML
static IEnumerable<string> ReadAsLines(string filename)
        {
            using (var reader = new StreamReader(filename))
                while (!reader.EndOfStream)
                    yield return reader.ReadLine();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900