Trying to create CSV file using VB.NET

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2006
Posts: 88
Reputation: bajanpoet is an unknown quantity at this point 
Solved Threads: 0
bajanpoet bajanpoet is offline Offline
Junior Poster in Training

Trying to create CSV file using VB.NET

 
0
  #1
Feb 23rd, 2009
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

  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Trying to create CSV file using VB.NET

 
0
  #2
Feb 23rd, 2009
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.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 88
Reputation: bajanpoet is an unknown quantity at this point 
Solved Threads: 0
bajanpoet bajanpoet is offline Offline
Junior Poster in Training

Re: Trying to create CSV file using VB.NET

 
0
  #3
Feb 23rd, 2009
Ok I'll check the table definition. I'll get back to you.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 88
Reputation: bajanpoet is an unknown quantity at this point 
Solved Threads: 0
bajanpoet bajanpoet is offline Offline
Junior Poster in Training

Re: Trying to create CSV file using VB.NET

 
0
  #4
Feb 23rd, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Trying to create CSV file using VB.NET

 
0
  #5
Feb 23rd, 2009
Very nice, I'm happy you solved it you're more than welcome
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: 4advanced is an unknown quantity at this point 
Solved Threads: 10
4advanced 4advanced is offline Offline
Junior Poster in Training

Re: Trying to create CSV file using VB.NET

 
0
  #6
Feb 23rd, 2009
*only for your attention*

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

  1. Using MyParser As Microsoft.VisualBasic.FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(filename, delimiter)
  2. End Using
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC