Solved

VB write data to csv

Posted on 2012-03-13
17
264 Views
Last Modified: 2012-03-14
I am trying to take records from an MS Access query in my windows application and create a csv with the records. Here is the code I have so far.

Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & GetPath() & "\" & DBName
        conn.Open()
        Dim csv As String = "\Paryoll.csv"
        Dim tw = System.IO.StreamWriter(GetPath() & csv)
        Dim sql = "SELECT * FROM qry_EarningsOT"
        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.GetDouble(3) & " , " & datRead.GetString(4) & " , " & datRead.GetDouble(5))
        Loop
        tw.Close()
        conn.Close()
0
Comment
Question by:rawilken
17 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 37718551
And what exactly is your question ? Isn't it working or do you get an error somewhere ?
0
 
LVL 9

Accepted Solution

by:
chwong67 earned 350 total points
ID: 37718847
I assume the database is MS Access.

Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & getpath() & "\" & DBName
        conn.Open()
        Dim csv As String = "\Paryoll.csv"
        Dim tw As New System.IO.StreamWriter(getpath() & csv)
        Dim sql = "SELECT * FROM qry_EarningsOT"
        Dim consql As New OleDbCommand
        consql.CommandText = sql
        consql.Connection = conn
        Dim datRead As OleDbDataReader
        datRead = consql.ExecuteReader
        Do Until datRead.Read = False

            tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
                         " , " & datRead.GetDouble(3) & " , " & datRead.GetString(4) & " , " & datRead.GetDouble(5))
        Loop
        tw.Close()
        conn.Close()

Open in new window

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37719589
0
 

Author Comment

by:rawilken
ID: 37719963
There error I get is that the streamwriter is a type and cannot be used as an expression...

 Public Function PrintDate(strExport As String) As Boolean
        Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & GetPath() & "\" & DBName
        conn.Open()
        Dim csv As String = "\Paryoll.csv"
        Dim tw = System.IO.StreamWriter(GetPath() & csv)
        Dim sql = "SELECT * FROM qry_EarningsOT"
        Dim consql As New OleDb.OleDbCommand
        consql.CommandText = sql
        consql.Connection = conn
        Dim datRead As OleDb.OleDbDataReader
        datRead = consql.ExecuteReader
        Do Until datRead.Read = False
            tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
                         " , " & datRead.GetDouble(3) & " , " & datRead.GetString(4) & " , " & datRead.GetDouble(5))
        Loop
        tw.Close()
        conn.Close()
    End Function
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37720057
I think you forgot a NEW:

Dim tw  AS NEW System.IO.StreamWriter(GetPath() & csv)
0
 

Author Comment

by:rawilken
ID: 37720264
Error Message...

Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
    'Public Sub New(path As String)': Argument matching parameter 'path' narrows from 'Object' to 'String'.
    'Public Sub New(stream As System.IO.Stream)': Argument matching parameter 'stream' narrows from 'Object' to 'System.IO.Stream'.      C:\Data Control\Time and Pay\Time_and_Pay\Time_and_Pay\TimeAndPay.vb      84      18      Time_and_Pay
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37720273
0
 

Author Comment

by:rawilken
ID: 37720738
I get no obvious error messages with the code written this way..

Dim tw As StreamWriter
tw = (GetPath() & csv)

I still need to test it though.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37720834
just try:

Dim tw As System.IO.TextWriter = New StreamWriter(GetPath() & csv)
0
 

Author Comment

by:rawilken
ID: 37721223
Two things..
1 I assume this overwrites the existing csv file if there is one
2 How do I append another dataset to the same csv
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37721328
0
 

Author Comment

by:rawilken
ID: 37721682
Error thrown...
error-1.JPG
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37721717
one of the value supposed to be double (fields 3 and 5) is not valid.

try this:
dim f3 as double
double.tryparse(datRead.getdouble(3).tostring, f3)

dim f5 as double
double.tryparse(datRead.getdouble(5).tostring, f5)

and use f3 and f5 in your .WriteLine
0
 

Author Comment

by:rawilken
ID: 37721778
Specified cast on f5 is not valid

Do Until datRead.Read = False
            Dim f3 As Double
            Double.TryParse(datRead.GetDouble(3).ToString, f3)
            Dim f5 As Double
            Double.TryParse(datRead.GetDouble(5).ToString, f5)
            tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
                         " , " & datRead.GetDouble(f3) & " , " & datRead.GetString(4) & " , " & datRead.GetDouble(f5))
        Loop

That field is a double, currency format in my database.
0
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 150 total points
ID: 37721884
change to :

 tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
                         " , " & f3 & " , " & datRead.GetString(4) & " , " & f5)
0
 

Author Comment

by:rawilken
ID: 37722044
Same error message...
Speficied case is not valid.
Double.TryParse(datRead.GetDouble(5).ToString, f5)
0
 

Author Closing Comment

by:rawilken
ID: 37722443
I also had to modify for null values where they existed.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now