Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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 existing row with new row. If not append the existing row. I tried in following 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
Posted
Updated 18-Jul-11 0:40am
v2

You seem to use string comparison for comparing dates. You should always use DateTime.Equals() to compare dates, string comparison is prone to errors.
And what is this code?
VB
_fileName.Replace(line, _newLine)

Why are you replacing it in the filename variable, you should be replacing it in the actual file content.
 
Share this answer
 
Comments
Member 8001800 18-Jul-11 6:51am    
So what is the suitable method in this case to replace the file contents?
[no name] 18-Jul-11 7:37am    
If the file is not very large, you can read the contents using StreamReader, make the replacements on the fly and then save the new contents using a StreamWriter.
Member 8001800 18-Jul-11 8:40am    
modified the while loop as below:

While line <> ""

line = reader.ReadLine

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

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

Dim newRow as string=line.Replace(line, _newLine)
Dim writer as new StreamWriter(_fileName)
writer.writeline(newRow)
writer.close()

End If

End If

End While

but while executing the Dim writer statement,getting exception as the file is being used by another process
Best suggestion I can make: stop using CSV. How much longer? As if XML is not enough (and much much more robust). Better yet, use Data Contract to stop this manual coding of persistence at all. See http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900