none
Writing to a csv file RRS feed

  • Question

  •  

    Hi all,

     

    I wonder if anyone can help.  I am fairly new to VB (about 5 months) so please forgive any obvious mistakes.

     

    I am trying to write some rows from my data table to a text file, as an export to another piece of software.

     

    I have written the following code, and as you will see, I am populating the items collection of a Combo Box, just to prove that the data is coming accross.  The Combo box fills correctly.  The file gets created, but remains empty.

     

    Any help / advice would be greatly appreciated.

     

    Code Begins ****************************************************************************

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

    tw = New StreamWriter("c:\test1.txt")

    tw.WriteLine("Cusinitial, Cussurname")

     

    Dim Conn As New SqlConnection

    Conn.ConnectionString = My.Settings.CEDConnectionString

    Dim Cmd As New SqlCommand

    Cmd.Connection = Conn

    Cmd.CommandText = "Select Surname, Wholename from Customer"

     

    Dim dr As SqlDataReader = Nothing

    Dim String1, String2 As String

     

    Try

    Conn.Open()

    dr = Cmd.ExecuteReader

    Do While (dr.Read())

    Me.ComboBox1.Items.Add(dr(0).ToString())

    String1 = dr.GetString(0)

    String2 = dr.GetString(1)

    tw.WriteLine(String1 & ", " & String2 & Chr(13))

    Console.WriteLine(dr.GetString(1))

    Loop

     

    Catch ex As Exception

    Finally

    If dr IsNot Nothing Then dr.Close()

    Conn.Close()

    End Try

     

    End Sub

     

    Many thanks in advance

     

    Graham

    Monday, September 17, 2007 5:42 PM

Answers

  • Hi Graham,

     

    You have fallen into a trap that many developers fall into. You forgot to flush and close your StreamWriter. What happens when you write to a StreamWriter is the information you write gets buffered and only once the buffer is full is its contents written to file. If you don't close the writer the information stays in the buffer.  You will do it again.

     

    tw.Close()  'automatically calls tw.Flush()

     

    Also your catching an Exception but your not doing anything with it. This is bad practise, if an exception occurred you'll never know about it. Even if you place a message box in your catch block to indicate something went wrong that would do. Don't do that in a final system though, generally you don't catch exception except if you want to log it, add additional information to it, to convert it to another exception, or you can handle the error condition and fix it.

     

     

     

    Monday, September 17, 2007 6:21 PM

All replies

  • Hi Graham,

     

    You have fallen into a trap that many developers fall into. You forgot to flush and close your StreamWriter. What happens when you write to a StreamWriter is the information you write gets buffered and only once the buffer is full is its contents written to file. If you don't close the writer the information stays in the buffer.  You will do it again.

     

    tw.Close()  'automatically calls tw.Flush()

     

    Also your catching an Exception but your not doing anything with it. This is bad practise, if an exception occurred you'll never know about it. Even if you place a message box in your catch block to indicate something went wrong that would do. Don't do that in a final system though, generally you don't catch exception except if you want to log it, add additional information to it, to convert it to another exception, or you can handle the error condition and fix it.

     

     

     

    Monday, September 17, 2007 6:21 PM
  • Hi Derek,

     

    Many thanks for that, it works a treat.

     

    I will write the exception catching code as well.

     

    Once again thank you.

     

    Tuesday, September 18, 2007 6:50 AM