?
Solved

VS 2008 C# Text File Export

Posted on 2011-10-20
8
Medium Priority
?
166 Views
Last Modified: 2013-12-16
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
0
Comment
Question by:rwheeler23
8 Comments
 
LVL 20

Assisted Solution

by:ddayx10
ddayx10 earned 600 total points
ID: 37004057
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1400 total points
ID: 37004059
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
 

Author Comment

by:rwheeler23
ID: 37004151
Interesting. I will see what people think of the current format. Thanks for the tip.
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
LVL 35

Expert Comment

by:Norie
ID: 37004213
Just curious, but how are the spaces being added to the data?

Or does the data come with spaces?
0
 

Author Comment

by:rwheeler23
ID: 37004442
The database is a MS database and all fields are defined as char not varchar.
0
 
LVL 35

Expert Comment

by:Norie
ID: 37004497
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
 

Author Comment

by:rwheeler23
ID: 37005938
I suppose I could add the trim function to every field in the query but I like your idea better.
0
 

Author Closing Comment

by:rwheeler23
ID: 37041112
Both answers helped.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question