Solved

CSVWriter from Data table wont write duplicte line

Posted on 2014-03-03
6
270 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
DevOps Toolchain Recommendations

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

 
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

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

Suggested Solutions

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…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 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

15 Experts available now in Live!

Get 1:1 Help Now