?
Solved

CSVWriter from Data table wont write duplicte line

Posted on 2014-03-03
6
Medium Priority
?
326 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

862 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