Link to home
Start Free TrialLog in
Avatar of Sirdots
Sirdots

asked on

Creating a flat file from a datatable using c#

I am working on creating a flat file from a datatable. I have not been able to get this to work. Attached is my specification and dotnet code. Also could you take a look at my sql and see what am doing wrong. The text file is generated but the columns are not in the right position.

Here is the sql

select
substring(Year, 1,4) as "AWARD-YEAR",
 substring(Social_Security_Number,1,9)as SSNO ,
 substring("Alternate ID",1, 10)as "id-alt",
 substring(Last_Name_Applicant,1,16)as "NAME-L",
substring(First_Name_Applicant,1,11)as "NAME-F",
substring(Middle_Initial_Applicant,1, 1)as "NAME-M",
 substring("Fund External Name", 1,35)as "fund_external_name",
 substring("Award Amount",1,5)as AWARDAMT
 from studentinfo
code.txt
Specification.JPG
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sirdots
Sirdots

ASKER

Thanks acperkins. Attached is the structure of the table. what I mean by "have not been able to get this to work" is that the columns did not align in the appropriate places. The output should follow what is stated in the attached specification document. There were so many spaces after each column.
I also dont want the field names to show if the flat file is generated. The field names appear on the first line.
tablestructure.JPG
It looks like all the columns are character data so it should return the right value.  As to the column headers, I don't do C#, but I suspect this is your culprit:
for (i = 0; i < dt.Columns.Count - 1; i++)                
        {  sw.Write(dt.Columns[i].ColumnName + " \t "); 
        }                
sw.Write(dt.Columns[i].ColumnName);                
sw.WriteLine();

Open in new window

Avatar of Sirdots

ASKER

I think I got it to work now. I will test now.
Avatar of Sirdots

ASKER

I will close this now. Thanks.