1

I have a csv file which have data like this:

Date,val1,val2,val3,val4,val5,val6,.......val47,val48
18/07/2011,0,0,0,10.98,0,0,0,10.98,10.98,0,0,0,0,0....,0,0

Now i need to compare the date value of existing row with new row's date value. If equal then need to replace the exisiting row with new row. If not append the existing row. I tried in follwing way. If dates are not equal, the new row is getting added but if dates are equal could not able to replace the existing row. Any suggestions please?????

Imports System.IO
Public Class Form1
    Private _fileName As String = "C:\Desktop\demo.csv"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim reader As New StreamReader(_fileName)
        Dim line As String = reader.ReadLine
        Dim _newLine As String = "18/07/2011,0,0,0,0,0,0,0,10.98,10.98,0,0,0,0,10.98,0,0,0,10.98"
        While line <> ""

            line = reader.ReadLine

            If line <> "" Then
                Dim values() As String = line.Split(",")

                If values(0) = "18/07/2011" Then

                    _fileName.Replace(line, _newLine)

                End If

            End If

        End While
        reader.Close()
    End Sub
End Class

1 Answer 1

1

The reason it's not being replaced is because you're modifying a string holding your filename location. However, this is NOT your actual file.

What I would suggest you do is construct a dictionary with the key as the date and read your file contents into that. If the dictionary contains a date already, then index the dictionary with that date and replace the values. Otherwise, just add to the dictionary. At the end of that process, clear out the file and write the contents of the dictionary to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.