I'm trying to create a *.csv file using the records stored in a table. Searching the Net, I came across a code snippet that I modified for my use. The relevant code is
If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
conn.Open()
csv = dlg.FileName
tw = New StreamWriter(csv)
Dim sql = "SELECT * FROM OTM_ORDERS"
Dim consql As New SqlCommand
consql.CommandText = sql
consql.Connection = conn
Dim datRead As SqlDataReader
datRead = consql.ExecuteReader
Do Until datRead.Read = False
tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
" , " & datRead.GetString(3) & " , " & datRead.GetString(4) & " , " & datRead.GetString(5) & _
" , " & datRead.GetString(6) & " , " & datRead.GetString(7))
Loop
tw.Close()
conn.Close()
End If
As I understand it, a Text Writer object streams text through a StreamWriter, and the text is saved in whatever file is linked to the StreamWriter. The above code is trying to pull each column into the file by using the datRead.GetString(n) functions and place a comma between each column effectively creating a csv file.
The error I get when trying to run this code is "Unable to cast object of type 'System.Int32' to type 'System.String'." The parameter is of type Int32, but it has to be a string. What am I doing wrong - or how can I attack this problem from a completely different angle?