VS 2008 C# Text File Export

I have this extraction program that creates a pipe delimited file. Right now it writes the records and preserves all spaces. I was wondering if there is a directive so will strip away all spaces or is there any other command to use when writing a text file that will do this?
foreach (DataRow dr_loopvariable in dt.Rows)
                {
                    dr = dr_loopvariable;
                    tw.WriteLine("{0,-10}{1,1}{2,-35}{3,1}{4,-35}{5,1}{6,-35}{7,1}{8,-35}{9,1}{10,-35}{11,1}{12,-35}{13,1}{14,-19}{15,1}{16,-2}{17,1}{18,-10}{19,1}{20,-10}{21,1}{22,-17}{23,1}{24,-13}{25,1}{26,-10}{27,1}{28,-4}{29,1}{30,-2}{31,1}{32,-30}{33,1}{34,-10}{35,1}{36,-13}", dr["VENDOR_ID"], P, dr["VENDOR_NAME"],P,dr["REMIT1"],P,dr["REMIT2"],P,dr["REMIT3"],P,dr["REMIT4"],P,dr["REMIT5"],P,dr["CITY"],P,dr["STATE"],P,dr["ZIP"],P,dr["CHECK_TOTAL"],P,dr["INVOICE_NUM"],P,dr["INV_NET_AMT"],P,dr["INV_DATE"],P,dr["DIVISION_ID"],P,dr["COUNTRY"],P,dr["DESCRIPTION"],P,dr["DISC_AMT"],P,dr["GROSS_INV_AMT"]);
                }

Open in new window

sample.txt
rwheeler23Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
I don't believe such a command exists. However, it could be simply a change to use string.Format rather than the WriteLine function's version. For example:

foreach (DataRow dr_loopvariable in dt.Rows)
{
    dr = dr_loopvariable;
    string line = string.Format("{0,-10}{1,1}{2,-35}{3,1}{4,-35}{5,1}{6,-35}{7,1}{8,-35}{9,1}{10,-35}{11,1}{12,-35}{13,1}{14,-19}{15,1}{16,-2}{17,1}{18,-10}{19,1}{20,-10}{21,1}{22,-17}{23,1}{24,-13}{25,1}{26,-10}{27,1}{28,-4}{29,1}{30,-2}{31,1}{32,-30}{33,1}{34,-10}{35,1}{36,-13}", dr["VENDOR_ID"], P, dr["VENDOR_NAME"],P,dr["REMIT1"],P,dr["REMIT2"],P,dr["REMIT3"],P,dr["REMIT4"],P,dr["REMIT5"],P,dr["CITY"],P,dr["STATE"],P,dr["ZIP"],P,dr["CHECK_TOTAL"],P,dr["INVOICE_NUM"],P,dr["INV_NET_AMT"],P,dr["INV_DATE"],P,dr["DIVISION_ID"],P,dr["COUNTRY"],P,dr["DESCRIPTION"],P,dr["DISC_AMT"],P,dr["GROSS_INV_AMT"]);
    tw.WriteLine(line.Replace(" ", string.Empty));
}

Open in new window

0
 
ddayx10Connect With a Mentor Commented:
Just a little fiddling around on my part but this may suit you <see code>.

I simplified your code for my convenience and used the response.write method instead of a textwriter(which I assume is what tw was)...

Anyway hope it helps.


DataTable dt = new DataTable();
        dt.Columns.Add("Column1");
        DataRow dr = dt.NewRow();
        dr["Column1"] = "Test With Spaces";
        dt.Rows.Add(dr);

         foreach (DataRow dr_loopvariable in dt.Rows)
         {
            dr = dr_loopvariable;
            Response.Write(String.Format("{0,-10}",dr["Column1"]).ToString().Replace(" ",""));
         }

Open in new window

0
 
rwheeler23Author Commented:
Interesting. I will see what people think of the current format. Thanks for the tip.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
NorieVBA ExpertCommented:
Just curious, but how are the spaces being added to the data?

Or does the data come with spaces?
0
 
rwheeler23Author Commented:
The database is a MS database and all fields are defined as char not varchar.
0
 
NorieVBA ExpertCommented:
So there fixed length.

I was just wondering if something like Trim could be used to get rid of any unwanted spaces, but I'm probably way off.:)
0
 
rwheeler23Author Commented:
I suppose I could add the trim function to every field in the query but I like your idea better.
0
 
rwheeler23Author Commented:
Both answers helped.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.