Solved

CSVWriter from Data table wont write duplicte line

Posted on 2014-03-03
6
280 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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