You must Sign In to post a response.
  • Category: .NET

    Only writing one column from a table while reading and writing it to txt using vb.net

    I have problem in display all column from a table in vb.net. I have 102 column but the when I run the program it only display one column.
    This is my code:
    Using streamWriterObject As New StreamWriter("C:\Users\Desktop\to.txt")
    While (myReader.HasRows)
    Dim i As Integer
    For i = 0 To 102
    myReader.Read()
    streamWriterObject.WriteLine(myReader(i).ToString() & " | ")
    i += 1
    Next
    End While
    myReader.Close()
    streamWriterObject.Close()
    End Using
  • #763809
    if myReader.HasRows then
    While (myReader.Read())
    streamWriterObject.WriteLine(myReader("ColumName").ToString() & " | ")
    End While
    myReader.Close()
    End IF
    streamWriterObject.Close()
    End Using

  • #763821
    Thanks ankush for the reply,
    if myReader.HasRows then
    While (myReader.Read())
    streamWriterObject.WriteLine(myReader("ColumName").ToString() & " | ")
    End While
    myReader.Close()
    End IF
    streamWriterObject.Close()
    End Using
    I have already try this code but it will need me to put the column name. If there is over an hundred column so I have to lists it all. Is there any way to do the job without listing all over an hundreds columns?
    Thanks in advance

  • #764157
    HI ronoer a ahong,

    You have to specify column names, otherwise how will you differentiate within column values written inside a file.
    You may try this also:

    //.. Assuming FName and LName are database retrieved columns ..
    streamWriterObject.WriteLine("FirstName: {0}, LastName : {1}", myReader("FName"), myReader("LName"))

    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com


Sign In to post your comments