Link to home
Start Free TrialLog in
Avatar of Graspdis
Graspdis

asked on

Text file From Datatable

Hi ,I have to develop the code(VB.NET)  which is to write the data into a textfile(.txt) from a datatable. i am having doubt on this one So,PLZ help me in this..My Datatable looks like dis...
   
    ID     Number     Name       State      city          zipcode
---------------------------------------------------------------------
    12              234        ABC         Ny         Ny           12345
    12              123        xyz           Ca        Sf             9567
    13              243        BYX          Tn        Ms            34567
    13              234        ABC          Oh       cleave      45678
    14              345        xyz            NZ        ny            78956
    14              243        BYX          PA        pi             23456

 But this shud be in this form ----
     ID          Number     Name       State      city        zipcode
---------------------------------------------------------------------
    12              234        ABC         Ny         Ny           12345
    13              234        ABC          Oh       cleave      45678
    12              123        xyz           Ca        Sf             9567
    13              243        BYX          Tn        Ms            34567
    14              243        BYX          PA        pi             23456
    14             345        xyz            NZ        ny            78956

The final datatabel shud be sorted according to the (Number and Name)..if there any rows which has same combination of those two fields that rows shud come one by one ..like first two having rows are having same number and name.. So my final datatable shud be like second datatble and from that datatable i have to write into a text file....

 i donno how to do get second datatable from the first datatable...So, PLZ help me in this

Thanking You




Avatar of adriankohws
adriankohws
Flag of Singapore image

Hi,

Simple, you can achieve this before even populating into the datatable by SQL Statement:

Say for example your table name is "MyTable"

SELECT * FROM MyTable
ORDER BY Number, Name
Avatar of prosh0t
prosh0t

If by chance you aren't populating your datatable from a database, you have to sort it using default views.
the following code illustrates what you need to do (it's in C#)

dTbl.DefaultView.Sort = "Number, Name Asc";
foreach(DataRowView dv in dTbl.DefaultView)
 {
         Console.WriteLine(dv["Number"].ToString();
}

for more info check out the documentation:
http://msdn2.microsoft.com/en-us/library/system.data.datarowview.row.aspx
ASKER CERTIFIED SOLUTION
Avatar of adriankohws
adriankohws
Flag of Singapore 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 Graspdis

ASKER

Thanks for ur solution.i have to try that thing populating into a gridview and sort it and writing into line by line . If plz can u explain it lil bit more and give any sample code to that will be helpfull for me...Thanks
Dim Str As String = ""
    'Create File if doesn't exist
        Dim FILE_NAME As String = "C:\temp\Custom.txt"
        If System.IO.File.Exists(FILE_NAME) = False Then
            System.IO.File.Create(FILE_NAME)
        End If

        Dim objWriter As System.IO.StreamWriter
        Try
            objWriter = New System.IO.StreamWriter(FILE_NAME)
        Catch ex As System.IO.IOException
            MsgBox("Please close the file: (C:\temp\Custom.txt) before proceeding" & vbCrLf & ex.Message.ToString, MsgBoxStyle.Exclamation)
            objWriter = Nothing
            Err = True
        End Try


'I assume you know how to write to text file.
'Say my datagridview is named "dgrid"

Dim x,y as integer

For x = 0 to dgrid.rows.count -1
    For y = 0 to dgrid.columns.count - 1
       Str = dgrid.Rows(x).Cells(y).Values & " "
   Next y
Next x

objWriter.Close()

Simple example, hope you can figure out, for sorting in datagridview, you can refer to MSDN:
http://msdn2.microsoft.com/en-us/library/0868ft3z.aspx

BTW, where you get this data from? A text file? Or a database table?
Actually i am getting the records from 5 queries..I am filling them in 5 different datatables and after that  i am combining all these 5 datatables into a single datatable. all those 5 queries have some common fields.
But in the text file the rows should come one by one according to that (number and name fields)
a datatable of 6 columns must be fetch from 5 tables? are you sure the database tables are designed correctly? but anyway, i am glad that I did helped
The datatables which i shown u are samples. My datatables are similar to this but not same. The text file shuld be one but the lines in that file are coming from 5 queiries. Some of the fields in all those queries are common.  In the sample datatable which i shown are having only common columns there are some other fields which are different for each query, the rows which is having id = 12 are coming from one query and rows having id= 13 are coming from another query...At last in the text file i have to display the records based on Number and Name.(i.e if any of the queries having same number and name that shud come one by one) again it starts from the first..Anyways thanks for ur help i vll try tat and vll get back to you if i have any doubts