Results 1 to 6 of 6

Thread: [02/03] fixed lenght file

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    138

    [02/03] fixed lenght file

    Hi gurus

    I have a little proyect to do, I need to query a sql database and get some information, I have this done using the datatable, now I need to get that information, loop through the datatable and write to the file every field that is on every row on the datatable.

    Here is where I need help, how do I loop through the datatale to get the fields.

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [02/03] fixed lenght file

    You'd probably want to refine this a little but it gives a pretty good idea:
    vb.net Code:
    1. Dim upperBound As Integer = table.Columns.Count - 1
    2. Dim writer As New IO.StreamWriter("file path here")
    3.  
    4. For Each row As DataRow In table.Rows
    5.     For columnIndex As Integer = 0 To upperBound
    6.         If columnIndex > 0 Then
    7.             writer.Write(",")
    8.         End If
    9.  
    10.         writer.Write(row(columnIndex).ToString())
    11.     Next
    12.  
    13.     writer.WriteLine()
    14. Next
    15.  
    16. writer.Close()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    138

    Re: [02/03] fixed lenght file

    Thanks big J.

    Is there a way that I can bring the filed name out of the row, something like

    Code:
    for each row in datatable
        string1=row("firstname")
        string2=row("lastname")
        string3=row("DOB")
     
       do some writting to the file
    
      next
    I still need to work on those values once the query is brought to the datatable or dataset.


    Thanks a bunch for the help.

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [02/03] fixed lenght file

    How about this one ?

    Code:
    Dim writer As New IO.StreamWriter("file path here")
    for each row in datatable
        string1=row("firstname")
        string2=row("lastname")
        string3=row("DOB")
     
    '   do some writting to the file
    writer.WriteLine(String.Format("{0}{1}{2}", string1, string2, string3))
      next
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    138

    Re: [02/03] fixed lenght file

    thanks D.

    so I CAN do this!!!!

    1. come with the query
    2. once the query's result is store on a datatable I can go and get the values of every fields on each row buy just bring the field name into the string and do some cool staff before writting to the file?

    Now I have another question

    datatable
    firstname lastname DOB
    Jack moore 3/3/2208
    mike janson 3/3/2209


    can I do this

    string1=row("dob")
    string2=row("firstname")
    string3=Row(lastname")

    and it will bring the right values

    Thanks again

  6. #6
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [02/03] fixed lenght file

    Quote Originally Posted by GUARO
    thanks D.


    string1=row("dob")
    string2=row("firstname")
    string3=Row(lastname")

    and it will bring the right values

    Thanks again
    yes it will
    Please mark you thread resolved using the Thread Tools as shown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width