Object array to string

U

Usarian Skiff

I'm making a csv file from a datatable. Here's my code

'Copy Headers
Dim obj(8) As Object
Dim txt As String
obj = dt.DefaultView.Item(0).Row.ItemArray
txt = Join(obj, ",")
filedata = txt

'Copy Data
For x = 0 To dt.DefaultView.Count - 1
filedata &= vbCrLf
obj = dt.DefaultView.Item(x).Row.ItemArray
txt = String.Format(",", obj)
filedata &= txt
Next

'Write File
Try
Dim savecsv As New StreamWriter(filename)
savecsv.Write(filedata)
savecsv.Close()
Catch ex As Exception
errormsg = ex.ToString
abort = True
End Try


I found that passing the datarow as an array into an array object will convert into a string using String.Join() to add in commas where apropriate. It works in the Headers section, but not in the Copy Data section. I get an error stating that I can't convert an array object into a string. Any tricks?

Usarian
 
U

Usarian Skiff

I changed my code before I uploaded it to try something new. Copy Data should read:

'Copy Data
For x = 0 To dt.DefaultView.Count - 1
filedata &= vbCrLf
obj = dt.DefaultView.Item(x).Row.ItemArray
txt = Join(obj, ",")
filedata &= txt
Next


I have also noticed that Join(obj, ",") and String.Join(",", obj) are not the same.. not sure why that is..



I'm making a csv file from a datatable. Here's my code

'Copy Headers
Dim obj(8) As Object
Dim txt As String
obj = dt.DefaultView.Item(0).Row.ItemArray
txt = Join(obj, ",")
filedata = txt

'Copy Data
For x = 0 To dt.DefaultView.Count - 1
filedata &= vbCrLf
obj = dt.DefaultView.Item(x).Row.ItemArray
txt = String.Format(",", obj)
filedata &= txt
Next

'Write File
Try
Dim savecsv As New StreamWriter(filename)
savecsv.Write(filedata)
savecsv.Close()
Catch ex As Exception
errormsg = ex.ToString
abort = True
End Try


I found that passing the datarow as an array into an array object will convert into a string using String.Join() to add in commas where apropriate. It works in the Headers section, but not in the Copy Data section. I get an error stating that I can't convert an array object into a string. Any tricks?

Usarian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top