| | |
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:
Solved Threads: 0
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
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?
VB.NET Syntax (Toggle Plain Text)
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?
the problem is in
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.
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Very nice, I'm happy you solved it
you're more than welcome
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Nov 2008
Posts: 63
Reputation:
Solved Threads: 10
*only for your attention*
I guess you should also check the TextFieldParser object :=)
I guess you should also check the TextFieldParser object :=)
VB.NET Syntax (Toggle Plain Text)
Using MyParser As Microsoft.VisualBasic.FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(filename, delimiter) End Using
![]() |
Similar Threads
- create an appointment in outlook using VB.NET (VB.NET)
- Create a link in a table read from a CSV file and pass a variable (PHP)
- How to read csv file contents in VB.Net (VB.NET)
- converting data in a table into csv (comma separated) file (VB.NET)
- Saving Excel Spreadsheet using ADO.net gives inconsistent results (C#)
Other Threads in the VB.NET Forum
- Previous Thread: Problem with Insert Statement
- Next Thread: How to check if a file is empty
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2005 2008 access arithmetic array assignment basic binary bing box button buttons center check code combobox component connectionstring convert cpu crystalreport data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dosconsolevb.net dropdownlist editvb.net excel file-dialog firewall folder ftp google hardcopy image images isnumericfuntioncall login math memory mobile ms mssqlbackend mysql navigate net networking opacity output peertopeervideostreaming picturebox1 port print printpreview problemwithinstallation project record reports" reuse save savedialog searchbox serial soap storedprocedure string tcp temp text textbox timer toolbox trim updown useraccounts usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet view vista visual visualbasic visualbasic.net visualstudio web wpf






