Solved

CSVWriter from Data table wont write duplicte line

Posted on 2014-03-03
6
265 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 142

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 142

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

17 Experts available now in Live!

Get 1:1 Help Now