943,725 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 11367
  • VB.NET RSS
Feb 23rd, 2009
0

Trying to create CSV file using VB.NET

Expand Post »
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

VB.NET Syntax (Toggle Plain Text)
  1. If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
  2. conn.Open()
  3. csv = dlg.FileName
  4. tw = New StreamWriter(csv)
  5. Dim sql = "SELECT * FROM OTM_ORDERS"
  6. Dim consql As New SqlCommand
  7. consql.CommandText = sql
  8. consql.Connection = conn
  9.  
  10. Dim datRead As SqlDataReader
  11. datRead = consql.ExecuteReader
  12. Do Until datRead.Read = False
  13. tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
  14. " , " & datRead.GetString(3) & " , " & datRead.GetString(4) & " , " & datRead.GetString(5) & _
  15. " , " & datRead.GetString(6) & " , " & datRead.GetString(7))
  16. Loop
  17. tw.Close()
  18. conn.Close()
  19. 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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bajanpoet is offline Offline
96 posts
since Sep 2006
Feb 23rd, 2009
0

Re: Trying to create CSV file using VB.NET

the problem is in datRead.GetString(X) you says give me string, he (VS) replies I don't have string, I've Int32 what shall I do for you.
Check the data types in your table if you have column of Int32 use GetInt32(X) or if you've column of bit use GetBoolean(X)

Hope it solve.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Feb 23rd, 2009
0

Re: Trying to create CSV file using VB.NET

Ok I'll check the table definition. I'll get back to you.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bajanpoet is offline Offline
96 posts
since Sep 2006
Feb 23rd, 2009
0

Re: Trying to create CSV file using VB.NET

Changed the GetString(n) to GetValue(n) and it worked! It didn't like the String so I looked at the code and realized I could send the value in its current format. Thanks for the help!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bajanpoet is offline Offline
96 posts
since Sep 2006
Feb 23rd, 2009
0

Re: Trying to create CSV file using VB.NET

Very nice, I'm happy you solved it you're more than welcome
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Feb 23rd, 2009
0

Re: Trying to create CSV file using VB.NET

*only for your attention*

I guess you should also check the TextFieldParser object :=)

VB.NET Syntax (Toggle Plain Text)
  1. Using MyParser As Microsoft.VisualBasic.FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(filename, delimiter)
  2. End Using
Reputation Points: 33
Solved Threads: 10
Junior Poster in Training
4advanced is offline Offline
67 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in VB.NET Forum Timeline: Problem with Insert Statement
Next Thread in VB.NET Forum Timeline: How to check if a file is empty





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC