Solved

CSVWriter from Data table wont write duplicte line

Posted on 2014-03-03
6
287 Views
Last Modified: 2014-03-08
Hi Experts,

I am having an issue with a CSV Writer that I am creating using a string builder, and a datatable when trying to increase or decrease a field value by +/- 1

Below is a part of my code.

This works... and yes, i know its in an infinate loop which.  This is how it is designed for the moment in order to confirm it is writing and will set the loop to the total number of rows shortly.

However...  what i am after is dr.item(5) is always an 1 or a 0 value.  What I need to do is write a duplicate line, with the opposing value so a 1 would change to a 0 and a 0 to a 1.

any and all help appreciated.

 'Location file will be saved
        Dim SaveLocation As String = "C:\Temp\CSV\"
        'File Name with the date and time the file was created for easy reference. 
        Dim filename As String = "TwoFour-" & DateTime.Now.ToString("ddmmyyy-HHmm") & ".csv"
        'StreamWriter to create, save and update the file as necessary - referencing the file name and the save location
        Dim sw As StreamWriter = File.AppendText(SaveLocation & filename)
        Dim i As Integer = 1
        Do Until i = 100
           

            'if the Save location does not exist then create it
            If Directory.Exists(SaveLocation) = False Then
                Directory.CreateDirectory(SaveLocation)
            End If




            'String builder 
            'Dim CSVString As New StringBuilder


            Dim dt As New DataTable
            With sqlcmd
                .Connection = sqlconn
                .CommandType = CommandType.Text
                .CommandText = "select * from dbo.Trn_Migration"
            End With

            Try

                sqlconn.ConnectionString = connstr
                sqlconn.Open()

                'sqldr = sqlcmd.ExecuteReader

                Using da As New SqlDataAdapter(sqlcmd)
                    da.Fill(dt)
                End Using

                For Each dr As DataRow In dt.Rows
                    Dim CSVString As New StringBuilder

                    'CSVString.Append(dr.Item(0) & "," & dr.Item(1) & "," & dr.Item(3) & "," & dr.Item(4) & "," & dr.Item(5) & "," & dr.Item(6) & "," & dr.Item(7) & "," & dr.Item(8) & "," & dr.Item(9))
                    CSVString.Append(dr.Item(0) & ",")
                    CSVString.Append(dr.Item(1) & ",")
                    CSVString.Append(dr.Item(2) & ",")
                    CSVString.Append(dr.Item(3) & ",")
                    CSVString.Append(dr.Item(4) & ",")
                    CSVString.Append(dr.Item(5) & ",")
                    CSVString.Append(dr.Item(6) & ",")
                    CSVString.Append(dr.Item(7) & ",")
                    CSVString.Append(dr.Item(8) & ",")
                    CSVString.Append(dr.Item(9) & ",")

                    ''TO DO 
                    ''CONDITION TO REPLICATE THE LINE FOR BUY SELL WITH TIMS ABSOLUTE VALUE. 


                    Dim CSVString1 As New StringBuilder
                    CSVString1.Append(dr.Item(0) & ",")
                    CSVString1.Append(dr.Item(1) & ",")
                    CSVString1.Append(dr.Item(2) & ",")
                    CSVString1.Append(dr.Item(3) & ",")
                    CSVString1.Append(dr.Item(4) & ",")
                    CSVString1.Append(dr.Item(5) & ",")
                    CSVString1.Append(dr.Item(6) & ",")
                    CSVString1.Append(dr.Item(7) & ",")
                    CSVString1.Append(dr.Item(8) & ",")
                    CSVString1.Append(dr.Item(9) & ", Duplicate Line")


                    sw.WriteLine(CSVString1)
                    sw.WriteLine(CSVString)

                    CSVString = Nothing
                    CSVString1 = Nothing



                Next
                sw.Close()
                'sqldr.Close()
                sqlconn.Close()
                'Threading.Thread.Sleep(10000)
            Catch ex As Exception

            End Try

            i = i
            
        Loop


    End Sub

Open in new window

0
Comment
Question by:SimonPrice33
  • 4
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39899818
so, in the second part of the code you want to do this?
CSVString1.Append( (dr.Item(5) == "0" ? "1" :  "0" )& ",") 

Open in new window

0
 

Author Comment

by:SimonPrice33
ID: 39899823
Hi Thanks for the quick response,

I will try this now and let you know how i get on
0
 

Author Comment

by:SimonPrice33
ID: 39899827
VS2010 is giving me an error on == saying an expression is expected, and the second "0" with syntax error.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39899909
I am sorry, I was writing C# code and not VB.net code, let's try that again, using IIF function:
http://msdn.microsoft.com/en-us/library/27ydhh0d%28v=vs.90%29.aspx
CSVString1.Append( IIf(dr.Item(5) = "0" , "1" ,  "0" ) & ",") 

Open in new window

0
 

Author Comment

by:SimonPrice33
ID: 39900065
i have gone with a differnet method of creating a new sub for now, and sending the infromation byval and then altering the data that way.

however i will try this again later to see if i can tidy up my code.
0
 

Author Closing Comment

by:SimonPrice33
ID: 39914387
.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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