IvanHowarth
asked on
What is the reverse to this code? - Reading and writing between Text file and DataView/DataSet
The following code sucessfully creates and writes the contents of a DataView (each value seperated by '#') to a Text file. Please can someone give me the code that will correctly read it back?
Private Sub SaveDBToFile()
Dim path As String = "C:\Temp\MyFile.txt"
'Create the file.
Dim fs As FileStream = File.Create(path)
Dim myDRV As DataRowView
For Each myDRV In MyDataView
For i As Integer = 0 To MyDataView.Table.Columns.C ount - 1
If (Not (myDRV(i) Is DBNull.Value)) Then
AddText(fs, CType(myDRV(i), String) + "#")
Else
AddText(fs, " #")
End If
Next
AddText(fs, Environment.NewLine)
Next
fs.Close()
End Sub
Private Shared Sub AddText(ByVal fs As FileStream, ByVal value As String)
Dim info As Byte() = New UTF8Encoding(True).GetByte s(value)
fs.Write(info, 0, info.Length)
End Sub
Private Sub SaveDBToFile()
Dim path As String = "C:\Temp\MyFile.txt"
'Create the file.
Dim fs As FileStream = File.Create(path)
Dim myDRV As DataRowView
For Each myDRV In MyDataView
For i As Integer = 0 To MyDataView.Table.Columns.C
If (Not (myDRV(i) Is DBNull.Value)) Then
AddText(fs, CType(myDRV(i), String) + "#")
Else
AddText(fs, " #")
End If
Next
AddText(fs, Environment.NewLine)
Next
fs.Close()
End Sub
Private Shared Sub AddText(ByVal fs As FileStream, ByVal value As String)
Dim info As Byte() = New UTF8Encoding(True).GetByte
fs.Write(info, 0, info.Length)
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Disregard the above error message, I have now moved the method call as the first line to avoid any conflict.
Nevertheless, I do get the following problem:
System.ArgumentNullExcepti on: Buffer cannot be null.
Parameter name: array
at System.IO.FileStream.Read( Byte[]arra y,Int32 offset, Int32 count)
...
Re wrote your array declaration to:
Dim bytes(CInt(fs.Length - 1)) As Byte
and then you get:
System.IndexOutOfRangeExce ption: Cannot find column 34.
at System.DataColumnCollectio n.get_Item (Int32 index)
at System.Data.DataRow.set_It em(Int32 columnIndex, Object value)
...
34 columns was written to file. fs.Length gives a value of 304229
Nevertheless, I do get the following problem:
System.ArgumentNullExcepti
Parameter name: array
at System.IO.FileStream.Read(
...
Re wrote your array declaration to:
Dim bytes(CInt(fs.Length - 1)) As Byte
and then you get:
System.IndexOutOfRangeExce
at System.DataColumnCollectio
at System.Data.DataRow.set_It
...
34 columns was written to file. fs.Length gives a value of 304229
ASKER
Problem found....
In my original code [AddText(fs, CType(myDRV(i), String) + "#") ] appended the # to the last column. Your code then looked for another column after this that didn't exist. I slightly modified your code so it didn't read that far:
sCols = Line1.Split(CChar("#"))
For y As Integer = 0 To sCols.Length - 2
...
Next
In my original code [AddText(fs, CType(myDRV(i), String) + "#") ] appended the # to the last column. Your code then looked for another column after this that didn't exist. I slightly modified your code so it didn't read that far:
sCols = Line1.Split(CChar("#"))
For y As Integer = 0 To sCols.Length - 2
...
Next
ASKER
The following error message keeps recurring (regardless if it's in a try-catch statement) and can only be stopped by killing the process via task manager:
System.IO.IOException: The process cannot access the file "C:\Temp\MyTest.txt" because it is being used by another process.
at System.IO._Error.WinIOErro
at System.IO.FileStream..ctor
Boolean useAsync, String msgPath, Boolean bFormProxy)
at System.IO.Stream..ctor(Str
at [MyApp]
(path is a variable containing the path of the file)
Any ideas?